forked from pegjs/pegjs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205c55d
commit 8346695
Showing
7 changed files
with
792 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
browser/* | ||
examples/*.js | ||
node_modules/* | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Start | ||
= Statements | ||
|
||
Statements | ||
= Statement* | ||
|
||
Statement | ||
= Indent* statement:(S / I) { return statement; } | ||
|
||
S | ||
= "S" EOS { | ||
return "S"; | ||
} | ||
|
||
I | ||
= "I" EOL ++Indent statements:Statements --Indent { return statements; } | ||
/ "I" EOS { return []; } | ||
|
||
Indent "indent" | ||
= "\t" | ||
/ !__ " " | ||
|
||
__ "white space" | ||
= " \t" | ||
/ " " | ||
|
||
EOS | ||
= EOL | ||
/ EOF | ||
|
||
EOL | ||
= "\n" | ||
|
||
EOF | ||
= !. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
I | ||
S | ||
I | ||
S | ||
S |
Oops, something went wrong.
8346695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The opcode error you mentioned in pegjs#217 (comment) is from generate-js.js#L386 or generate-js.js#L708. Your getting this error because the predicate operations are first converted to PEG.js bytecode before being generated into parse functions.
If you don't want to mess with the bytecode or JavaScript generators, I suggest adding a expansion based transform pass in the compiler:
const newRules = [];
visitor.js
look forincrement_match
anddecrement_match
n
) for rules that will be incremented or decrementedn
, generate the AST for new rules:Indent$3 = Indent Indent Indent
newRules.push( Indent$3 );
rule_ref
node:const reference = new RuleRef( Indent$3.name, node.location );
swapExpression( node, reference );
ast.rules.push( newRules );
You should also throw a fatal warning when a
decrement_match
takesn
below 0, as this will have been cause by extradecrement_match
expressions.If you want, I can submit a PR that should add the 2 (check and transform) passes to get this feature working.
8346695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@futagoza Thanks a lot! Please submit the PR :) I will also take a look at your fork. Will take me a little while to get up to speed on the internals and writing lexers/grammars/ASTs again. Been a long time since Computer Science. Thanks again!!
8346695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kristianmandrup, sorry for the late reply, been preoccupied with a few things.
I've managed to implement a pass that does what I described above, but it seems the implementation above
only works on rules that have 1 expression that's a
expression *
orexpression +
, and then replaces the++ expression
or-- expression
with the expanded result. Not what I was aiming for 🤣I suggest digging into the bytecode or js generators to see if you or someone who can understand them can implement this in one of them. If afraid I can't help with the parser generator my self as I'm more of a language designer then a language implementer, and the code in there just baffles me right now, but I'm still studying it part time 😄
Update
I've pushed the changes into a new branch in my copy of pegjs: futagoza/pegjs/commits/indentation
You might want to look at the first 3 commits I did before taking a look at the relevant ON-TOPIC commit 😝