Skip to content

Commit

Permalink
i44 | fix: add rune and string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sirkon committed May 5, 2021
1 parent f7c07d3 commit b1a63ed
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
23 changes: 21 additions & 2 deletions internal/ast/targets.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ast

import (
"fmt"
"strconv"
)

// Target ...
type Target struct {
Type TargetEnum
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions internal/generator/gogen/internal/srcobj/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 23 additions & 6 deletions internal/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions internal/srcbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit b1a63ed

Please sign in to comment.