diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-06-20 18:39:38 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-06-20 18:46:13 +0900 |
commit | 8993406a8ebe8c0a01d5081dc4afcf819e3160d4 (patch) | |
tree | 8940654c0bbc1e200908cafe83813fe7765a5229 /grammar | |
parent | Add syntax of comments (diff) | |
download | cotia-8993406a8ebe8c0a01d5081dc4afcf819e3160d4.tar.gz cotia-8993406a8ebe8c0a01d5081dc4afcf819e3160d4.tar.xz |
Add syntax of modifiers and actions
Currently, a mode modifier and push/pop actions are available.
The modifier and the actions make sense in only lexical specifications.
Diffstat (limited to 'grammar')
-rw-r--r-- | grammar/grammar.go | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/grammar/grammar.go b/grammar/grammar.go index 2a7953a..4507ddd 100644 --- a/grammar/grammar.go +++ b/grammar/grammar.go @@ -29,10 +29,51 @@ func NewGrammar(root *spec.RootNode) (*Grammar, error) { return nil, err } + var modes []mlspec.LexModeName + if prod.Modifier != nil { + mod := prod.Modifier + switch mod.Name { + case "mode": + if mod.Parameter == "" { + return nil, fmt.Errorf("modifier 'mode' needs a parameter") + } + modes = []mlspec.LexModeName{ + mlspec.LexModeName(mod.Parameter), + } + default: + return nil, fmt.Errorf("invalid modifier name '%v'", mod.Name) + } + } + + alt := prod.RHS[0] + var push mlspec.LexModeName + var pop bool + if alt.Action != nil { + act := alt.Action + switch act.Name { + case "push": + if act.Parameter == "" { + return nil, fmt.Errorf("action 'push' needs a parameter") + } + push = mlspec.LexModeName(act.Parameter) + case "pop": + if act.Parameter != "" { + return nil, fmt.Errorf("action 'pop' needs no parameter") + } + pop = true + default: + return nil, fmt.Errorf("invalid action name '%v'", act.Name) + } + } + entries = append(entries, &mlspec.LexEntry{ + Modes: modes, Kind: mlspec.LexKind(prod.LHS), - Pattern: mlspec.LexPattern(prod.RHS[0].Elements[0].Pattern), + Pattern: mlspec.LexPattern(alt.Elements[0].Pattern), + Push: push, + Pop: pop, }) + continue } |