From b1a63ed7ff85be372fea7fd51cb311055ee0d1fc Mon Sep 17 00:00:00 2001 From: Denis Cheremisov Date: Wed, 5 May 2021 03:22:57 +0300 Subject: [PATCH] i44 | fix: add rune and string validation --- internal/ast/targets.go | 23 +++++++++++++-- .../generator/gogen/internal/srcobj/file.go | 16 ++++++++-- internal/listener/listener.go | 29 +++++++++++++++---- internal/srcbuilder/builder.go | 10 ++++--- 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/internal/ast/targets.go b/internal/ast/targets.go index 33d6d72..5a7af4f 100644 --- a/internal/ast/targets.go +++ b/internal/ast/targets.go @@ -1,5 +1,10 @@ package ast +import ( + "fmt" + "strconv" +) + // Target ... type Target struct { Type TargetEnum @@ -20,15 +25,29 @@ func (t *Target) SetClose() { } // SetChar sets target into Char -func (t *Target) SetChar(text string) { +func (t *Target) SetChar(text string) error { + // TODO get rid of this after https://github.com/antlr/antlr4/pull/2642 will be merged + if _, err := strconv.Unquote(text); err != nil { + return fmt.Errorf("process rune %s: %w", text, err) + } + t.Type = Char t.Value = text + + return nil } // SetString sets target into TypeName -func (t *Target) SetString(text string) { +func (t *Target) SetString(text string) error { + // TODO get rid of this after https://github.com/antlr/antlr4/pull/2642 will be merged + if _, err := strconv.Unquote(text); err != nil { + return fmt.Errorf("process string %s: %w", text, err) + } + t.Type = String t.Value = text + + return nil } // SetLimit sets target limit diff --git a/internal/generator/gogen/internal/srcobj/file.go b/internal/generator/gogen/internal/srcobj/file.go index 130d1ce..4808b75 100644 --- a/internal/generator/gogen/internal/srcobj/file.go +++ b/internal/generator/gogen/internal/srcobj/file.go @@ -5,11 +5,13 @@ import ( "fmt" "io" "sort" + "strconv" + "strings" "github.com/sirkon/errors" "github.com/sirkon/gosrcfmt" - "github.com/sirkon/ldetool/internal/generator" + "github.com/sirkon/message" ) // File represents LDE generated Go source file @@ -60,7 +62,10 @@ func (f *File) AddNamedImport(access, path string) error { if prevAccess, ok := f.imports[path]; ok && prevAccess != access { return fmt.Errorf( `attempt to use "%s" as '%s' while it was added with '%s' access name before"`, - path, access, prevAccess) + path, + access, + prevAccess, + ) } f.imports[path] = access return nil @@ -159,6 +164,13 @@ func (f *File) Dump(w io.Writer) error { res, err := gosrcfmt.Autogen(buf.Bytes()) if err != nil { + lines := strings.Split(buf.String(), "\n") + format := "%0" + strconv.Itoa(len(strconv.Itoa(len(lines)))) + "d %s" + for i, line := range lines { + // need to use i + 3 as gosrcfmt.Autogen puts 3 lines in the head + message.Errorf(format, i+3, line) + } + return errors.Wrap(err, "format autogenerated code") } if _, err := io.Copy(w, bytes.NewBuffer(res)); err != nil { diff --git a/internal/listener/listener.go b/internal/listener/listener.go index 742b257..5912604 100644 --- a/internal/listener/listener.go +++ b/internal/listener/listener.go @@ -7,7 +7,6 @@ import ( "strconv" "github.com/antlr/antlr4/runtime/Go/antlr" - "github.com/sirkon/ldetool/internal/ast" "github.com/sirkon/ldetool/internal/parser" "github.com/sirkon/ldetool/internal/types" @@ -30,7 +29,8 @@ func checkReserved(token antlr.Token) { return } panic( - fmt.Sprintf("%d:%d \033[1m%s\033[0m is reserved identifier", + fmt.Sprintf( + "%d:%d \033[1m%s\033[0m is reserved identifier", token.GetLine(), token.GetColumn()+1, token.GetText(), @@ -567,9 +567,23 @@ func (l *Listener) EnterTargetLit(ctx *parser.TargetLitContext) { l.seq().Append(a) } if ctx.StringLit() != nil { - l.target.SetString(ctx.StringLit().GetText()) + if err := l.target.SetString(ctx.StringLit().GetText()); err != nil { + panic(fmt.Sprintf( + "%d:%d %s", + ctx.CharLit().GetSymbol().GetLine(), + ctx.CharLit().GetSymbol().GetColumn()+1, + err, + )) + } } else if ctx.CharLit() != nil { - l.target.SetChar(ctx.CharLit().GetText()) + if err := l.target.SetChar(ctx.CharLit().GetText()); err != nil { + panic(fmt.Sprintf( + "%d:%d %s", + ctx.CharLit().GetSymbol().GetLine(), + ctx.CharLit().GetSymbol().GetColumn()+1, + err, + )) + } } else { panic("Integerity error") } @@ -594,8 +608,11 @@ func (l *Listener) EnterBound(ctx *parser.BoundContext) { } if upper < lower { token := ctx.IntLit(1).GetSymbol() - panic(fmt.Sprintf("%d:%d upper bound must be greater than lower", - token.GetLine(), token.GetColumn()+1)) + panic(fmt.Sprintf( + "%d:%d upper bound must be greater than lower", + token.GetLine(), + token.GetColumn()+1, + )) } l.target.SetBound(lower, upper) } diff --git a/internal/srcbuilder/builder.go b/internal/srcbuilder/builder.go index 98529eb..98ebfdf 100644 --- a/internal/srcbuilder/builder.go +++ b/internal/srcbuilder/builder.go @@ -2,16 +2,15 @@ package srcbuilder import ( "fmt" - "github.com/sirkon/message" "io" "strings" "github.com/antlr/antlr4/runtime/Go/antlr" "github.com/sirkon/gotify" "github.com/sirkon/ldetool/internal/ast" - "github.com/sirkon/ldetool/internal/types" - "github.com/sirkon/ldetool/internal/generator" + "github.com/sirkon/ldetool/internal/types" + "github.com/sirkon/message" ) var _ ast.ActionDispatcher = &SrcBuilder{} @@ -81,7 +80,10 @@ func (sb *SrcBuilder) BuildRule(rule *ast.Rule) (err error) { // Build full source file func (sb *SrcBuilder) Build() (err error) { - sb.gen.Generate(sb.pkgName, sb.dest) + if err := sb.gen.Generate(sb.pkgName, sb.dest); err != nil { + return err + } + return nil }