diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-31 20:10:26 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-31 20:10:26 +0900 |
commit | ccf0123d7f1b88ee7cdd4e2ea15ab9e94457538a (patch) | |
tree | 6ad35758eb1fd37a84d33266af54a4e9bd6d405e /driver/parser.go | |
parent | Refactor (diff) | |
download | urubu-ccf0123d7f1b88ee7cdd4e2ea15ab9e94457538a.tar.gz urubu-ccf0123d7f1b88ee7cdd4e2ea15ab9e94457538a.tar.xz |
Remove the expected terminals field from the parsing table
The driver searches the expected terminals corresponding to each state if necessary.
Diffstat (limited to 'driver/parser.go')
-rw-r--r-- | driver/parser.go | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/driver/parser.go b/driver/parser.go index 8cd6511..91f364e 100644 --- a/driver/parser.go +++ b/driver/parser.go @@ -185,7 +185,7 @@ ACTION_LOOP: Col: tok.Col, Message: "unexpected token", Token: tok, - ExpectedTerminals: p.expectedTerms(p.top()), + ExpectedTerminals: p.searchLookahead(p.top()), }) ok := p.trapError() @@ -456,28 +456,33 @@ func (p *Parser) SyntaxErrors() []*SyntaxError { return p.synErrs } -func (p *Parser) expectedTerms(state int) []string { +func (p *Parser) searchLookahead(state int) []string { kinds := []string{} - terms := p.gram.ParsingTable.ExpectedTerminals[state] + term2Kind := p.gram.LexicalSpecification.Maleeni.TerminalToKind + kindNames := p.gram.LexicalSpecification.Maleeni.Spec.KindNames aliases := p.gram.LexicalSpecification.Maleeni.KindAliases - for _, tsym := range terms { + termCount := p.gram.ParsingTable.TerminalCount + base := p.top() * termCount + for term := 0; term < termCount; term++ { + if p.gram.ParsingTable.Action[base+term] == 0 { + continue + } + // We don't add the error symbol to the look-ahead symbols because users cannot input the error symbol // intentionally. - if tsym == p.gram.ParsingTable.ErrorSymbol { + if term == p.gram.ParsingTable.ErrorSymbol { continue } - if tsym == p.gram.ParsingTable.EOFSymbol { + if term == p.gram.ParsingTable.EOFSymbol { kinds = append(kinds, "<eof>") continue } - if alias := aliases[tsym]; alias != "" { + if alias := aliases[term]; alias != "" { kinds = append(kinds, alias) } else { - term2Kind := p.gram.LexicalSpecification.Maleeni.TerminalToKind - kindNames := p.gram.LexicalSpecification.Maleeni.Spec.KindNames - kinds = append(kinds, kindNames[term2Kind[tsym]].String()) + kinds = append(kinds, kindNames[term2Kind[term]].String()) } } |