Skip to content

Commit

Permalink
Allow nonterminal in lookahead sets
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jul 26, 2017
1 parent fa8f004 commit ec2e2c0
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 60 deletions.
4 changes: 2 additions & 2 deletions baselines/reference/es6.grammar.emu.html
Expand Up @@ -352,9 +352,9 @@
</emu-production>
<emu-production name="EscapeSequence" type="lexical">
<emu-rhs a="e9e86f07"><emu-nt>CharacterEscapeSequence</emu-nt></emu-rhs>
<emu-rhs a="3651e0da">
<emu-rhs a="6964a19d">
<emu-t>0</emu-t>
<emu-gann>lookahead <emu-nt>DecimalDigit</emu-nt></emu-gann>
<emu-gann>lookahead <emu-nt>DecimalDigit</emu-nt></emu-gann>
</emu-rhs>
<emu-rhs a="a8071b85"><emu-nt>HexEscapeSequence</emu-nt></emu-rhs>
<emu-rhs a="44bd6f55"><emu-nt>UnicodeEscapeSequence</emu-nt></emu-rhs>
Expand Down
2 changes: 1 addition & 1 deletion baselines/reference/es6.grammar.html
Expand Up @@ -632,7 +632,7 @@
<a name="e9e86f07"></a><span class="rhs"><span class="nonterminal"><a href="#CharacterEscapeSequence">CharacterEscapeSequence</a></span></span>
</div>
<div class="rhs-list-item">
<a name="3651e0da"></a><span class="rhs"><span class="terminal">0</span> <span class="assertion">[<span class="keyword">lookahead</span> <span class="nonterminal"><a href="#DecimalDigit">DecimalDigit</a></span>]</span></span>
<a name="6964a19d"></a><span class="rhs"><span class="terminal">0</span> <span class="assertion">[<span class="keyword">lookahead</span> <span class="nonterminal"><a href="#DecimalDigit">DecimalDigit</a></span>]</span></span>
</div>
<div class="rhs-list-item">
<a name="a8071b85"></a><span class="rhs"><span class="nonterminal"><a href="#HexEscapeSequence">HexEscapeSequence</a></span></span>
Expand Down
2 changes: 1 addition & 1 deletion baselines/reference/es6.grammar.md
Expand Up @@ -251,7 +251,7 @@

&emsp;&emsp;<a name="EscapeSequence"></a>*EscapeSequence* **::**
&emsp;&emsp;&emsp;<a name="EscapeSequence-e9e86f07"></a>*[CharacterEscapeSequence](#CharacterEscapeSequence)*
&emsp;&emsp;&emsp;<a name="EscapeSequence-3651e0da"></a>`` 0 ``&emsp;[lookahead *[DecimalDigit](#DecimalDigit)*]
&emsp;&emsp;&emsp;<a name="EscapeSequence-6964a19d"></a>`` 0 ``&emsp;[lookahead *[DecimalDigit](#DecimalDigit)*]
&emsp;&emsp;&emsp;<a name="EscapeSequence-a8071b85"></a>*[HexEscapeSequence](#HexEscapeSequence)*
&emsp;&emsp;&emsp;<a name="EscapeSequence-44bd6f55"></a>*[UnicodeEscapeSequence](#UnicodeEscapeSequence)*

Expand Down
2 changes: 1 addition & 1 deletion baselines/reference/es6.grammar.nodes
Expand Up @@ -1055,7 +1055,7 @@
(265,6)SyntaxKind[LookaheadAssertion]
(265,6)SyntaxKind[OpenBracketToken]
(265,7)SyntaxKind[LookaheadKeyword]
(265,17)SyntaxKind[ExclamationEqualsToken]
(265,17)SyntaxKind[LessThanExclamationToken]
(265,20)SyntaxKind[SymbolSpan]
(265,20)SyntaxKind[Nonterminal]
(265,20)SyntaxKind[Identifier](text = "DecimalDigit")
Expand Down
2 changes: 1 addition & 1 deletion baselines/reference/es6.grammar.tokens
Expand Up @@ -873,7 +873,7 @@ SyntaxKind[LineTerminatorToken](265,1): «line terminator»
SyntaxKind[Terminal](265,2): `0`
SyntaxKind[OpenBracketToken](265,6): [
SyntaxKind[LookaheadKeyword](265,7): lookahead
SyntaxKind[ExclamationEqualsToken](265,17): !=
SyntaxKind[LessThanExclamationToken](265,17): <!
SyntaxKind[Identifier](265,20): DecimalDigit
SyntaxKind[CloseBracketToken](265,32): ]
SyntaxKind[LineTerminatorToken](266,1): «line terminator»
Expand Down
3 changes: 3 additions & 0 deletions baselines/reference/test.grammar.emu.html
Expand Up @@ -41,4 +41,7 @@
<emu-production name="Z">
<del><emu-rhs a="6dcd4ce2"><emu-nt>A</emu-nt></emu-rhs></del>
<ins><emu-rhs a="ae4f281d"><emu-nt>B</emu-nt></emu-rhs></ins>
</emu-production>
<emu-production name="LookaheadRestriction">
<emu-rhs a="5c1008f6"><emu-gann>lookahead ∉ <emu-nt>A</emu-nt></emu-gann></emu-rhs>
</emu-production>
8 changes: 8 additions & 0 deletions baselines/reference/test.grammar.html
Expand Up @@ -64,4 +64,12 @@
</div>
</div>
</div>
<a name="LookaheadRestriction"></a><div class="production">
<span class="nonterminal">LookaheadRestriction</span><span class="punctuation"> :</span>
<div class="rhs-list">
<div class="rhs-list-item">
<a name="5c1008f6"></a><span class="rhs"><span class="assertion">[<span class="keyword">lookahead</span><span class="nonterminal"><a href="#A">A</a></span>]</span></span>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions baselines/reference/test.grammar.md
Expand Up @@ -24,4 +24,7 @@
&emsp;&emsp;<a name="Z"></a>*Z* **:**
&emsp;&emsp;&emsp;<del><a name="Z-6dcd4ce2"></a>*[A](#A)*</del>
&emsp;&emsp;&emsp;<ins><a name="Z-ae4f281d"></a>*[B](#B)*</ins>

&emsp;&emsp;<a name="LookaheadRestriction"></a>*LookaheadRestriction* **:**
&emsp;&emsp;&emsp;<a name="LookaheadRestriction-5c1008f6"></a>[lookahead ∉ *[A](#A)*]

15 changes: 15 additions & 0 deletions baselines/reference/test.grammar.nodes
Expand Up @@ -174,3 +174,18 @@
(22,10)SyntaxKind[Nonterminal]
(22,10)SyntaxKind[Identifier](text = "B")
(23,1)SyntaxKind[DedentToken]
(24,1)SyntaxKind[Production]
(24,1)SyntaxKind[Identifier](text = "LookaheadRestriction")
(25,1)SyntaxKind[RightHandSideList]
(25,1)SyntaxKind[IndentToken]
(25,5)SyntaxKind[RightHandSide]
(25,5)SyntaxKind[SymbolSpan]
(25,5)SyntaxKind[LookaheadAssertion]
(25,5)SyntaxKind[OpenBracketToken]
(25,6)SyntaxKind[LookaheadKeyword]
(25,16)SyntaxKind[LessThanExclamationToken]
(25,19)SyntaxKind[SymbolSpan]
(25,19)SyntaxKind[Nonterminal]
(25,19)SyntaxKind[Identifier](text = "A")
(25,20)SyntaxKind[CloseBracketToken]
(25,21)SyntaxKind[DedentToken]
13 changes: 12 additions & 1 deletion baselines/reference/test.grammar.tokens
Expand Up @@ -118,4 +118,15 @@ SyntaxKind[LineTerminatorToken](22,1): «line terminator»
SyntaxKind[Identifier](22,10): B
SyntaxKind[LineTerminatorToken](23,1): «line terminator»
SyntaxKind[DedentToken](23,1): «dedent»
SyntaxKind[EndOfFileToken](23,1): «EndOfFileToken»
SyntaxKind[LineTerminatorToken](24,1): «line terminator»
SyntaxKind[Identifier](24,1): LookaheadRestriction
SyntaxKind[ColonToken](24,21): :
SyntaxKind[LineTerminatorToken](25,1): «line terminator»
SyntaxKind[IndentToken](25,1): «indent»
SyntaxKind[OpenBracketToken](25,5): [
SyntaxKind[LookaheadKeyword](25,6): lookahead
SyntaxKind[LessThanExclamationToken](25,16): <!
SyntaxKind[Identifier](25,19): A
SyntaxKind[CloseBracketToken](25,20): ]
SyntaxKind[DedentToken](25,21): «dedent»
SyntaxKind[EndOfFileToken](25,21): «EndOfFileToken»
2 changes: 1 addition & 1 deletion grammars/es2015.grammar
Expand Up @@ -262,7 +262,7 @@ LineContinuation ::

EscapeSequence ::
CharacterEscapeSequence
`0` [lookahead != DecimalDigit]
`0` [lookahead <! DecimalDigit]
HexEscapeSequence
UnicodeEscapeSequence

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "grammarkdown",
"version": "1.0.7",
"version": "1.0.8",
"description": "Markdown-like DSL for defining syntactic grammars for programming languages.",
"license": "Apache-2.0",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion spec/es6.grammar
Expand Up @@ -262,7 +262,7 @@ LineContinuation ::

EscapeSequence ::
CharacterEscapeSequence
`0` [lookahead != DecimalDigit]
`0` [lookahead <! DecimalDigit]
HexEscapeSequence
UnicodeEscapeSequence

Expand Down
3 changes: 3 additions & 0 deletions spec/test.grammar
Expand Up @@ -20,3 +20,6 @@ F[A] :
Z :
<del>A</del>
<ins>B</ins>

LookaheadRestriction:
[lookahead <! A]
30 changes: 24 additions & 6 deletions src/lib/checker.ts
Expand Up @@ -484,7 +484,7 @@ export class Checker {
return this.reportGrammarError(node.symbol.pos, Diagnostics._0_expected, tokenToString(SyntaxKind.LineTerminatorToken));
}

if (node.next && node.next.kind === SyntaxKind.Prose) {
if (node.next && node.next.symbol.kind === SyntaxKind.Prose) {
return this.reportGrammarError(node.next.pos, Diagnostics._0_expected, tokenToString(SyntaxKind.LineTerminatorToken));
}

Expand Down Expand Up @@ -619,7 +619,7 @@ export class Checker {
case SyntaxKind.ElementOfToken:
case SyntaxKind.LessThanExclamationToken:
case SyntaxKind.NotAnElementOfToken:
return this.reportGrammarError(node.operatorToken.end, Diagnostics._0_expected, tokenToString(SyntaxKind.OpenBraceToken));
return this.reportGrammarError(node.operatorToken.end, Diagnostics._0_expected, formatList([SyntaxKind.OpenBraceToken, SyntaxKind.Nonterminal]));
}
}

Expand All @@ -628,10 +628,9 @@ export class Checker {
case SyntaxKind.EqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.NotEqualToToken:
if (node.lookahead.kind !== SyntaxKind.SymbolSpan) {
if (!isTerminal(node.lookahead)) {
return this.reportGrammarErrorForNode(node, Diagnostics._0_expected, formatList([
SyntaxKind.Terminal,
SyntaxKind.Identifier,
SyntaxKind.UnicodeCharacterLiteral
]));
}
Expand All @@ -642,14 +641,33 @@ export class Checker {
case SyntaxKind.ElementOfToken:
case SyntaxKind.LessThanExclamationToken:
case SyntaxKind.NotAnElementOfToken:
if (node.lookahead.kind !== SyntaxKind.SymbolSet) {
return this.reportGrammarErrorForNode(node, Diagnostics._0_expected, tokenToString(SyntaxKind.OpenBraceToken));
if (!isNonterminalOrSymbolSet(node.lookahead)) {
return this.reportGrammarErrorForNode(node, Diagnostics._0_expected, formatList([
SyntaxKind.OpenBraceToken,
SyntaxKind.Nonterminal
]));
}

break;
}

return false;

function isTerminal(node: SymbolSpan | SymbolSet) {
if (node.kind !== SyntaxKind.SymbolSpan) return false;
switch (node.symbol.kind) {
case SyntaxKind.Terminal:
case SyntaxKind.UnicodeCharacterLiteral:
return true;
}
return false;
}

function isNonterminalOrSymbolSet(node: SymbolSpan | SymbolSet) {
return node.kind === SyntaxKind.SymbolSpan
? node.symbol.kind === SyntaxKind.Nonterminal
: node.kind === SyntaxKind.SymbolSet;
}
}

private checkSymbolSet(node: SymbolSet): void {
Expand Down

0 comments on commit ec2e2c0

Please sign in to comment.