diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-07 01:35:58 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-07 02:17:41 +0900 |
commit | a57fda765cd32b44cd069da1c9a442b701b36dc2 (patch) | |
tree | e8f72fbcac83fc18525263051e50d2b17f11ef24 | |
parent | Call the 'MissError' when input doesn't meet an error production (diff) | |
download | urubu-a57fda765cd32b44cd069da1c9a442b701b36dc2.tar.gz urubu-a57fda765cd32b44cd069da1c9a442b701b36dc2.tar.xz |
Pass a token that caused a syntax error to the semantic action APIs
-rw-r--r-- | driver/parser.go | 6 | ||||
-rw-r--r-- | driver/semantic_action.go | 21 | ||||
-rw-r--r-- | driver/semantic_action_test.go | 6 |
3 files changed, 17 insertions, 16 deletions
diff --git a/driver/parser.go b/driver/parser.go index 3099d73..c9b6229 100644 --- a/driver/parser.go +++ b/driver/parser.go @@ -137,7 +137,7 @@ ACTION_LOOP: } if tok.EOF { if p.semAct != nil { - p.semAct.MissError() + p.semAct.MissError(tok) } return nil @@ -157,7 +157,7 @@ ACTION_LOOP: count, ok := p.trapError() if !ok { if p.semAct != nil { - p.semAct.MissError() + p.semAct.MissError(tok) } return nil @@ -174,7 +174,7 @@ ACTION_LOOP: p.shift(act * -1) if p.semAct != nil { - p.semAct.TrapAndShiftError(count) + p.semAct.TrapAndShiftError(tok, count) } } } diff --git a/driver/semantic_action.go b/driver/semantic_action.go index 5bec385..1db703b 100644 --- a/driver/semantic_action.go +++ b/driver/semantic_action.go @@ -22,13 +22,14 @@ type SemanticActionSet interface { Accept() // TrapAndShiftError runs when the driver traps a syntax error and shifts a error symbol onto the state stack. - // `n` is the number of frames that the driver discards from the state stack. - // Unlike `Shift` function, this function doesn't take a token as an argument because a token corresponding to - // the error symbol doesn't exist. - TrapAndShiftError(n int) - - // MissError runs when the driver fails to trap a syntax error. - MissError() + // `cause` is a token that caused a syntax error. `popped` is the number of frames that the driver discards + // from the state stack. + // Unlike `Shift` function, this function doesn't take a token to be shifted as an argument because a token + // corresponding to the error symbol doesn't exist. + TrapAndShiftError(cause *mldriver.Token, popped int) + + // MissError runs when the driver fails to trap a syntax error. `cause` is a token that caused a syntax error. + MissError(cause *mldriver.Token) } var _ SemanticActionSet = &SyntaxTreeActionSet{} @@ -204,8 +205,8 @@ func (a *SyntaxTreeActionSet) Accept() { a.ast = top[0].ast } -func (a *SyntaxTreeActionSet) TrapAndShiftError(n int) { - a.semStack.pop(n) +func (a *SyntaxTreeActionSet) TrapAndShiftError(cause *mldriver.Token, popped int) { + a.semStack.pop(popped) errSym := a.gram.ParsingTable.ErrorSymbol @@ -228,7 +229,7 @@ func (a *SyntaxTreeActionSet) TrapAndShiftError(n int) { }) } -func (a *SyntaxTreeActionSet) MissError() { +func (a *SyntaxTreeActionSet) MissError(cause *mldriver.Token) { } func (a *SyntaxTreeActionSet) CST() *Node { diff --git a/driver/semantic_action_test.go b/driver/semantic_action_test.go index c3f8943..1d785e4 100644 --- a/driver/semantic_action_test.go +++ b/driver/semantic_action_test.go @@ -37,11 +37,11 @@ func (a *testSemAct) Accept() { a.actLog = append(a.actLog, "accept") } -func (a *testSemAct) TrapAndShiftError(n int) { - a.actLog = append(a.actLog, fmt.Sprintf("trap/%v/shift/error", n)) +func (a *testSemAct) TrapAndShiftError(cause *mldriver.Token, popped int) { + a.actLog = append(a.actLog, fmt.Sprintf("trap/%v/shift/error", popped)) } -func (a *testSemAct) MissError() { +func (a *testSemAct) MissError(cause *mldriver.Token) { a.actLog = append(a.actLog, "miss") } |