Comments (5)
These are unintended consequences of #160 -- because we now run checks of each stage without throwing exceptions, the AST in reportInfiniteRepetition
pass can have infinity recursive rules which we try to check. The fast solution is to factor out the reportInfiniteRecursion
pass to its own stage before the current check
stage. The more clever solution is to tweak all passes in a check
stage so that they will not assume that AST contains no recursive rules.
from peggy.
Tip: if you need only start and end offset, more performant solution is use range()
function.
Typical solution to parse arithmetic expressions:
//{ The higher the priority, the larger the rule number.
Expr// assignment, minimal priority
= l:Expr0 t:(p1 Expr0)* {return rightAssotiative(l, t);};
Expr0// value lists
= h:(@Expr1 comma)* t:Expr1? {return list(h, t);};
Expr1// Logical OR
= l:Expr2 t:(p2 Expr2)* {return leftAssotiative(l, t);};
Expr2// Logical AND
= l:Expr3 t:(p3 Expr3)* {return leftAssotiative(l, t);};
Expr3// Bitwise OR
= l:Expr4 t:(p4 Expr4)* {return leftAssotiative(l, t);};
Expr4// Bitwise XOR
= l:Expr5 t:(p5 Expr5)* {return leftAssotiative(l, t);};
Expr5// bitwise AND
= l:Expr6 t:(p6 Expr6)* {return leftAssotiative(l, t);};
Expr6// comparison operators
= l:Expr7 t:(p7 Expr7)* {return leftAssotiative(l, t);};
Expr7// relational operators
= l:Expr8 t:(p8 Expr8)* {return leftAssotiative(l, t);};
Expr8// bitwise shift
= l:Expr9 t:(p9 Expr9)* {return leftAssotiative(l, t);};
Expr9// addition and substraction
= l:Expr10 t:(p10 Expr10)* {return leftAssotiative(l, t);};
Expr10// multiplication, division, remainder
= l:Expr11 t:(p11 Expr11)* {return leftAssotiative(l, t);};
Expr11// pow
= l:Expr12 t:(p12 Expr12)* {return leftAssotiative(l, t);};
Expr12// Logical and bitwise NOT, unary operators, maximum priority
= o:p13 r:Expr12 {return node({kind: o, expr: r});}
/ Expr13
;
Expr13// prefix increment / decrement
= o:p14 r:Expr13 {return node({kind: o, expr: r});}
/ Expr14
;
Expr14// postfix increment / decrement
= r:Expr15 o:p15 {return node({kind: o, expr: r});}
/ Expr15
;
Expr15// function call, indexation, dot syntax
= /*l:Expr16 t:suffix* {return suffix(l, t);};
Expr16
= funcdef
/ block
/ literal
/ constant
/ IfExpr
/ ForExpr
/ WhileExpr
/ BreakExpr
/ YieldExpr
/ ReturnExpr
/ ContinueExpr
/ define
/ name
/ */"(" @Expr ")"
;
//}
//{ Operators
comma = @',' _;
p1 = @('='/'+='/'-='/'*='/'/='/'%='/'<<='/'>>='/'&='/'^='/'|='/'&&='/'||=') _;
p2 = @'||'_;
p3 = @'&&'_;
p4 = @'|' _;
p5 = @'^' _;
p6 = @'&' _;
p7 = @('=='/'!=') _;
p8 = @('<='/'<'/'>='/'>') _;
p9 = @('<<'/'>>') _;
p10= @[+-] _;
p11= @[*/%]_;
p12= @'**' _;
p13= !p14 @[!~+-] _;
p14= @('++'/'--') _;
p15= p14;
//}
_ 'Whitespace' = [ \r\n\t]*;
Error parsing grammar
Maximum call stack size exceeded
This is bad. Could you post the full minimal reproducible example of such grammar? It could be bug in peggy looks like some infinity recursion was not detected
from peggy.
Thanks for tip and grammar pattern!
from peggy.
This is bad. Could you post the full minimal reproducible example of such grammar? It could be bug in peggy looks like some infinity recursion was not detected
Narrowed it, this is minimal, or close to minimal that causes maximum stack exceeded error:
start = expr*
expr
= expr "++"
from peggy.
@Mingun do you have a suggestion for how to catch this as an infinite loop?
from peggy.
Related Issues (20)
- When grammer file to long getting ERR_STRING_TOO_LONG HOT 2
- Question about behavior of repetitions HOT 1
- Document the error.format() method - how is this supposed to work? HOT 4
- Refer to specific grammar rule HOT 6
- Return an expression or decide how to continue HOT 4
- Proposal to improve readability of generate-js.js HOT 4
- Because version 3.0 already released, it's time to remove this option
- Release 3.02 HOT 1
- [docs] Parens create a pluck context HOT 7
- Run tests on es modules HOT 3
- Conversion of a pegjs file to BNF or vice versa ? HOT 2
- Type of `ParserBuildOptions` is wrong HOT 3
- [feature request] watch HOT 3
- doc example doesn't correspond to code example HOT 3
- Fix all uses of String.prototype.substr HOT 5
- Performance javascript grammar HOT 7
- test output format HOT 4
- Documentation improvements HOT 5
- use `expression1 / expression2 / ... / expressionn` throw `peg$SyntaxError: Expected end of input but "d" found.` HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peggy.