Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use to parse JavaScript

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use to parse JavaScript, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Talk

Because recently there is a need to use code that parses JavaScript, most cases can be handled using regular expression matching, but once it depends on the content of the code context, regular or simple character parsing is not enough, and you need a language parser to get the entire AST (abstract syntax tree).

Then I found several JavaScript parsers written in JavaScript:

Esprima

Acorn

UglifyJS 2

Shift

From the submission record, the maintenance is quite good, the various development features of ES can keep up, I have a brief understanding of them, talk about some of them.

Esprima is a classic parser, and Acorn was born a few years ago. According to the authors of Acorn, building the wheel was more fun at the time, with a speed comparable to that of Esprima, but with less code to implement. The key point is that the AST results from the two parsers (yes, only the AST,tokens is different) are consistent with the The Estree Spec specification (this is the JavaScript AST specification document for the output of the SpiderMonkey engine given by Mozilla engineers, also refer to: SpiderMonkey in MDN), that is, the results are largely compatible.

Now the famous Webpack parsing code also uses Acorn.

As for Uglify, a well-known JavaScript code compressor, it actually comes with a code parser, which can also output AST, but its function is more used to compress the code, and it doesn't feel pure enough to parse the code.

Shift didn't know much about this, except that he defined his own set of AST specifications.

There is a performance test on Esprima's official website. The results of my running on chrome are as follows:

> "|" + "|"-"|" * "|" / "|"% "| |" | "^" | "&" | "in" | "instanceof"}

AssignmentExpression

The operator attribute represents an assignment operator, and left and right are expressions around the assignment operator.

Interface AssignmentExpression

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report