Skip to content

Commit b1a63ed

Browse files
committed
i44 | fix: add rune and string validation
1 parent f7c07d3 commit b1a63ed

4 files changed

Lines changed: 64 additions & 14 deletions

File tree

internal/ast/targets.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package ast
22

3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
38
// Target ...
49
type Target struct {
510
Type TargetEnum
@@ -20,15 +25,29 @@ func (t *Target) SetClose() {
2025
}
2126

2227
// SetChar sets target into Char
23-
func (t *Target) SetChar(text string) {
28+
func (t *Target) SetChar(text string) error {
29+
// TODO get rid of this after https://github.com/antlr/antlr4/pull/2642 will be merged
30+
if _, err := strconv.Unquote(text); err != nil {
31+
return fmt.Errorf("process rune %s: %w", text, err)
32+
}
33+
2434
t.Type = Char
2535
t.Value = text
36+
37+
return nil
2638
}
2739

2840
// SetString sets target into TypeName
29-
func (t *Target) SetString(text string) {
41+
func (t *Target) SetString(text string) error {
42+
// TODO get rid of this after https://github.com/antlr/antlr4/pull/2642 will be merged
43+
if _, err := strconv.Unquote(text); err != nil {
44+
return fmt.Errorf("process string %s: %w", text, err)
45+
}
46+
3047
t.Type = String
3148
t.Value = text
49+
50+
return nil
3251
}
3352

3453
// SetLimit sets target limit

internal/generator/gogen/internal/srcobj/file.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"fmt"
66
"io"
77
"sort"
8+
"strconv"
9+
"strings"
810

911
"github.com/sirkon/errors"
1012
"github.com/sirkon/gosrcfmt"
11-
1213
"github.com/sirkon/ldetool/internal/generator"
14+
"github.com/sirkon/message"
1315
)
1416

1517
// File represents LDE generated Go source file
@@ -60,7 +62,10 @@ func (f *File) AddNamedImport(access, path string) error {
6062
if prevAccess, ok := f.imports[path]; ok && prevAccess != access {
6163
return fmt.Errorf(
6264
`attempt to use "%s" as '%s' while it was added with '%s' access name before"`,
63-
path, access, prevAccess)
65+
path,
66+
access,
67+
prevAccess,
68+
)
6469
}
6570
f.imports[path] = access
6671
return nil
@@ -159,6 +164,13 @@ func (f *File) Dump(w io.Writer) error {
159164

160165
res, err := gosrcfmt.Autogen(buf.Bytes())
161166
if err != nil {
167+
lines := strings.Split(buf.String(), "\n")
168+
format := "%0" + strconv.Itoa(len(strconv.Itoa(len(lines)))) + "d %s"
169+
for i, line := range lines {
170+
// need to use i + 3 as gosrcfmt.Autogen puts 3 lines in the head
171+
message.Errorf(format, i+3, line)
172+
}
173+
162174
return errors.Wrap(err, "format autogenerated code")
163175
}
164176
if _, err := io.Copy(w, bytes.NewBuffer(res)); err != nil {

internal/listener/listener.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strconv"
88

99
"github.com/antlr/antlr4/runtime/Go/antlr"
10-
1110
"github.com/sirkon/ldetool/internal/ast"
1211
"github.com/sirkon/ldetool/internal/parser"
1312
"github.com/sirkon/ldetool/internal/types"
@@ -30,7 +29,8 @@ func checkReserved(token antlr.Token) {
3029
return
3130
}
3231
panic(
33-
fmt.Sprintf("%d:%d \033[1m%s\033[0m is reserved identifier",
32+
fmt.Sprintf(
33+
"%d:%d \033[1m%s\033[0m is reserved identifier",
3434
token.GetLine(),
3535
token.GetColumn()+1,
3636
token.GetText(),
@@ -567,9 +567,23 @@ func (l *Listener) EnterTargetLit(ctx *parser.TargetLitContext) {
567567
l.seq().Append(a)
568568
}
569569
if ctx.StringLit() != nil {
570-
l.target.SetString(ctx.StringLit().GetText())
570+
if err := l.target.SetString(ctx.StringLit().GetText()); err != nil {
571+
panic(fmt.Sprintf(
572+
"%d:%d %s",
573+
ctx.CharLit().GetSymbol().GetLine(),
574+
ctx.CharLit().GetSymbol().GetColumn()+1,
575+
err,
576+
))
577+
}
571578
} else if ctx.CharLit() != nil {
572-
l.target.SetChar(ctx.CharLit().GetText())
579+
if err := l.target.SetChar(ctx.CharLit().GetText()); err != nil {
580+
panic(fmt.Sprintf(
581+
"%d:%d %s",
582+
ctx.CharLit().GetSymbol().GetLine(),
583+
ctx.CharLit().GetSymbol().GetColumn()+1,
584+
err,
585+
))
586+
}
573587
} else {
574588
panic("Integerity error")
575589
}
@@ -594,8 +608,11 @@ func (l *Listener) EnterBound(ctx *parser.BoundContext) {
594608
}
595609
if upper < lower {
596610
token := ctx.IntLit(1).GetSymbol()
597-
panic(fmt.Sprintf("%d:%d upper bound must be greater than lower",
598-
token.GetLine(), token.GetColumn()+1))
611+
panic(fmt.Sprintf(
612+
"%d:%d upper bound must be greater than lower",
613+
token.GetLine(),
614+
token.GetColumn()+1,
615+
))
599616
}
600617
l.target.SetBound(lower, upper)
601618
}

internal/srcbuilder/builder.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package srcbuilder
22

33
import (
44
"fmt"
5-
"github.com/sirkon/message"
65
"io"
76
"strings"
87

98
"github.com/antlr/antlr4/runtime/Go/antlr"
109
"github.com/sirkon/gotify"
1110
"github.com/sirkon/ldetool/internal/ast"
12-
"github.com/sirkon/ldetool/internal/types"
13-
1411
"github.com/sirkon/ldetool/internal/generator"
12+
"github.com/sirkon/ldetool/internal/types"
13+
"github.com/sirkon/message"
1514
)
1615

1716
var _ ast.ActionDispatcher = &SrcBuilder{}
@@ -81,7 +80,10 @@ func (sb *SrcBuilder) BuildRule(rule *ast.Rule) (err error) {
8180

8281
// Build full source file
8382
func (sb *SrcBuilder) Build() (err error) {
84-
sb.gen.Generate(sb.pkgName, sb.dest)
83+
if err := sb.gen.Generate(sb.pkgName, sb.dest); err != nil {
84+
return err
85+
}
86+
8587
return nil
8688
}
8789

0 commit comments

Comments
 (0)