From 88f83624dc6d7c3b66a34c7c3f414719530e421f Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sat, 17 Apr 2021 22:51:06 +0900 Subject: Add validation of lexical specs and improve error messages --- compiler/compiler.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'compiler/compiler.go') diff --git a/compiler/compiler.go b/compiler/compiler.go index 02cda43..15f42f3 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1,6 +1,7 @@ package compiler import ( + "fmt" "io" "strings" @@ -26,6 +27,11 @@ type compilerConfig struct { } func Compile(lexspec *spec.LexSpec, opts ...compilerOption) (*spec.CompiledLexSpec, error) { + err := lexspec.Validate() + if err != nil { + return nil, fmt.Errorf("invalid lexical specification:\n%w", err) + } + config := &compilerConfig{ logger: log.NewNopLogger(), } @@ -36,10 +42,10 @@ func Compile(lexspec *spec.LexSpec, opts ...compilerOption) (*spec.CompiledLexSp } } - var kinds []string + var kinds []spec.LexKind var patterns map[int][]byte { - kinds = append(kinds, "") + kinds = append(kinds, spec.LexKindNil) patterns = map[int][]byte{} for i, e := range lexspec.Entries { kinds = append(kinds, e.Kind) -- cgit v1.2.3