I
I
Interface2017-12-15 00:36:11
JavaScript
Interface, 2017-12-15 00:36:11

What is the best way to write a javascript-based language parser?

I want to make a simple language with the ability to insert pieces of js. (Ala jsx)
What is the best way to do this? How do different languages ​​integrate into each other. For example html -> js, jsx -> js, php -> html etc.?
I note that I do not need AST from JavaScript, I need to define sections with it. Those. the output could be something like this:

node
- type='block'
- children [
    node
      - type="js"
      - text="return foo"
...

I see the following ways:
1) you can somehow try to solve the problem without parsing the js itself, but this leads to problems.
Problems

In the case of html, this is expressed in the fact that the construction will be recognized, not as I would like<script>console.log('</script>')</script>

2) using parsers with configurable grammar:
For example, Yacc, jison , chevrotain - I really like this option, but I don’t want to “bicycle” the parsing of js itself, moreover, I want to have its grammar from external sources + constantly updated. If you have in mind, for example, a maintained repository with es6+ grammar, that would be great.
3) use parsers sharpened on js: acorn ,
esprima, or maybe you suggest doing it through babel's plugins for example. If you have any articles/projects in mind that describe how to build a superset of js this way, I'd be grateful. In this case, very good support for the language (and its not yet fully supported features) comes out of the box, which is so lacking in the 2nd version;
3.5) I suppose that you can teach any of the parsers to parse the string while he can, and when he can not read anything further - consider this the end of the js snippet. How correct is this approach? Moreover, it is desirable to dwell on tokenization (because js ast, as I said, I do not need)
4) Do not parse js completely, but only break it into "dangerous" tokens (lines, regular expressions, comments) - code sections where the "closing tag / bracket" may appear - to be honest, I don't like this option, but if there is very cheap solution in this vein - I will be glad to link.
In summary:
1) do you know where to find a grammar (es6, ideally with a live community) js that can be extended and fed to a "live" (not abandoned) parser?
2) is there any experience / bookmarked article on how this can be done differently?
3) it is possible to integrate a parser with a free grammar (jison for example) with for example acorn. It would be great if they gave a tip where to dig, where to read?
Tips are welcome.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mikhail Potanin, 2017-12-18
@potan

Use monodic/combinatorial parsers . Examples are usually given in Haskell, but using this pattern is not difficult in almost any language - closures and garbage collection are desirable, they can be dispensed with, but programming is difficult.

S
SimAllll, 2017-12-15
@SimAllll

How do different languages ​​integrate into each other in drgua. For example html -> js

2 absolutely separate libraries - one for HTML, the other some kind of JS engine with binding
, the first one basically works, and how it finds the <script> element - it pulls the second
one in php - there the parser doesn’t give a damn about what’s around it, at least html at least brainfuck, it only looks for <?php <? ?> etc.

S
Stalker_RED, 2017-12-15
@Stalker_RED

1. <script>console.log('</script>')</script>Such a construction in the browser can (could before?) cause problems. However, it's not too difficult to write a simple analyzer that will determine that these things are inside quotes.
3.jslint?

P
Philipp, 2017-12-15
@zoonman

What you are looking for is called AST. acorn and esprima are exactly what you are looking for.
There is also https://developer.mozilla.org/en-US/docs/Mozilla/P...
I recommend you to take https://github.com/syntax-tree/hast and get nodes with JavaScript and them in turn to feed JS AST.

N
NonameProgrammer, 2017-12-18
@NonameProgrammer

As far as I understand, you need to make your own language, but under js? If yes, then you can try to make a translator of your language (syntax) and translate it into js. Take as a basis any js implementation of your taste and language. And add your syntax constructs or whatever

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question