From c313f7870bd547534c7c7bb0ad01003ab9983b34 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Tue, 16 Feb 2021 00:07:40 +0900 Subject: Add types of lexical specifications APIs of compiler and driver packages use these types. Because CompiledLexSpec struct a lexer takes has kind names of lexical specification entries, the lexer sets them to tokens. --- compiler/compiler.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'compiler/compiler.go') diff --git a/compiler/compiler.go b/compiler/compiler.go index 153ad77..3ae647e 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1,9 +1,29 @@ package compiler -func Compile(regexps map[int][]byte) (*DFA, error) { - root, symTab, err := parse(regexps) +import "github.com/nihei9/maleeni/spec" + +func Compile(lexspec *spec.LexSpec) (*spec.CompiledLexSpec, error) { + var kinds []string + var patterns map[int][]byte + { + kinds = append(kinds, "") + patterns = map[int][]byte{} + for i, e := range lexspec.Entries { + kinds = append(kinds, e.Kind) + patterns[i+1] = []byte(e.Pattern) + } + } + root, symTab, err := parse(patterns) + if err != nil { + return nil, err + } + dfa := genDFA(root, symTab) + tranTab, err := genTransitionTable(dfa) if err != nil { return nil, err } - return genDFA(root, symTab), nil + return &spec.CompiledLexSpec{ + Kinds: kinds, + DFA: tranTab, + }, nil } -- cgit v1.2.3