From 8993406a8ebe8c0a01d5081dc4afcf819e3160d4 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sun, 20 Jun 2021 18:39:38 +0900 Subject: 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. --- grammar/grammar.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'grammar/grammar.go') 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 } -- cgit v1.2.3