Skip to content

Commit 0c87684

Browse files
committed
Improve parser API
1 parent 306bbb9 commit 0c87684

File tree

8 files changed

+43
-35
lines changed

8 files changed

+43
-35
lines changed

interpreter/interpreter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func TestMapExpr(t *testing.T) {
437437
}
438438

439439
func testEval(t *testing.T, input string) runtime.Object {
440-
nodes, _, _ := syntax.Parse([]byte(input))
440+
nodes := syntax.Parse([]byte(input))
441441
if err := nodes.Err(); err != nil {
442442
t.Fatalf("%s\n %s", input, err.Error())
443443
}

k3/t3xf/compiler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestCompiler(t *testing.T) {
345345
if tt.skip {
346346
t.Skip()
347347
}
348-
root, _, _ := syntax.Parse([]byte(tt.input))
348+
root := syntax.Parse([]byte(tt.input))
349349
if root.Err() != nil {
350350
t.Fatalf("syntax.Parse(%q) returned error: %v", tt.input, root.Err())
351351
}

repl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func repl() error {
2121
break
2222
}
2323

24-
root, _, _ := syntax.Parse([]byte(s.Text()))
24+
root := syntax.Parse([]byte(s.Text()))
2525
if err := root.Err(); err != nil {
2626
fmt.Println(err.Error())
2727
continue

ttcn3/format/format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (p *CanonicalPrinter) Fprint(v interface{}) error {
7474
// The simple formatting rules do not need much context information.
7575
// This allows us to use the tokeniser and release initial
7676
// formatting experiments even before the parser is ready.
77-
n, _, _ := syntax.Parse(b)
77+
n := syntax.Parse(b)
7878

7979
// Only pretty print if there are no syntax errors.
8080
if n.Err() != nil {

ttcn3/syntax/nodes_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestFindChildOf(t *testing.T) {
2727
}
2828
for _, tt := range tests {
2929
input, cursor := ntttest.CutCursor(tt.input)
30-
root, _, _ := syntax.Parse([]byte(input), syntax.WithFilename(tt.input))
30+
root := syntax.Parse([]byte(input), syntax.WithFilename(tt.input))
3131
actual := printNode(syntax.FindChildOf(root, cursor))
3232
if actual != tt.want {
3333
t.Errorf("FindChildOf(%q) = %q, want %q", tt.input, actual, tt.want)

ttcn3/syntax/parser.go

+34-28
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,32 @@ type Mode uint
2222

2323
type ParserOption func(*parser) error
2424

25+
// WithFilename sets the filename of the source code.
2526
func WithFilename(filename string) ParserOption {
2627
return func(p *parser) error {
2728
p.Filename = filename
2829
return nil
2930
}
3031
}
3132

33+
// WithNames sets the names map in which the parser will store the names of the
34+
// parsed entities.
35+
func WithNames(names map[string]bool) ParserOption {
36+
return func(p *parser) error {
37+
p.names = names
38+
return nil
39+
}
40+
}
41+
42+
// WithUses sets the uses map in which the parser will store the uses of the
43+
// parsed entities.
44+
func WithUses(uses map[string]bool) ParserOption {
45+
return func(p *parser) error {
46+
p.uses = uses
47+
return nil
48+
}
49+
}
50+
3251
const (
3352
PedanticSemicolon = 1 << iota // expect semicolons pedantically
3453
IgnoreComments // ignore comments
@@ -48,9 +67,6 @@ func NewParser(src []byte) *parser {
4867
p.ppDefs["0"] = false
4968
p.ppDefs["1"] = true
5069

51-
p.names = make(map[string]bool)
52-
p.uses = make(map[string]bool)
53-
5470
p.Root = newRoot(src)
5571

5672
// fetch first token
@@ -60,24 +76,8 @@ func NewParser(src []byte) *parser {
6076
return &p
6177
}
6278

63-
// Parse the source code of a single file and return the corresponding syntax
64-
// tree. The source code may be provided via the filename of the source file,
65-
// or via the src parameter.
66-
//
67-
// If src != nil, Parse parses the source from src and the filename is only
68-
// used when recording position information. The type of the argument for the
69-
// src parameter must be string, []byte, or io.Reader. If src == nil, Parse
70-
// parses the file specified by filename.
71-
//
72-
// The mode parameter controls the amount of source text parsed and other
73-
// optional parser functionality.
74-
//
75-
// If the source couldn't be read, the returned AST is nil and the error
76-
// indicates the specific failure. If the source was read but syntax errors
77-
// were found, the result is a partial AST (with Bad* nodes representing the
78-
// fragments of erroneous source code). Multiple errors are returned via a
79-
// ErrorList which is sorted by file position.
80-
func Parse(src []byte, opts ...ParserOption) (root *Root, names map[string]bool, uses map[string]bool) {
79+
// Parse parses the source code of a TTCN-3 module and returns the corresponding AST.
80+
func Parse(src []byte, opts ...ParserOption) (root *Root) {
8181

8282
region := trc.StartRegion(context.Background(), "syntax.Parse")
8383
defer region.End()
@@ -86,7 +86,7 @@ func Parse(src []byte, opts ...ParserOption) (root *Root, names map[string]bool,
8686
for _, opt := range opts {
8787
if err := opt(p); err != nil {
8888
p.Root.errs = append(p.Root.errs, err)
89-
return p.Root, p.names, p.uses
89+
return p.Root
9090
}
9191
}
9292
for p.tok != EOF {
@@ -102,7 +102,7 @@ func Parse(src []byte, opts ...ParserOption) (root *Root, names map[string]bool,
102102
}
103103

104104
}
105-
return p.Root, p.names, p.uses
105+
return p.Root
106106
}
107107

108108
// If src != nil, readSource converts src to a []byte if possible;
@@ -1009,7 +1009,9 @@ func (p *parser) parseName() *Ident {
10091009
switch p.tok {
10101010
case IDENT, ADDRESS, CONTROL, CLASS:
10111011
id := &Ident{Tok: p.consume(), IsName: true}
1012-
p.names[id.String()] = true
1012+
if p.names != nil {
1013+
p.names[id.String()] = true
1014+
}
10131015
return id
10141016
}
10151017
p.expect(IDENT)
@@ -1278,7 +1280,7 @@ func (p *parser) addName(n Node) {
12781280
p.addName(n)
12791281
}
12801282
default:
1281-
if name := Name(n); name != "" {
1283+
if name := Name(n); p.names != nil && name != "" {
12821284
p.names[name] = true
12831285
}
12841286
}
@@ -1293,10 +1295,14 @@ func (p *parser) make_use(toks ...Token) *Ident {
12931295
panic("No support for multi-token identifiers.")
12941296
}
12951297
id := &Ident{Tok: toks[0]}
1296-
p.uses[toks[0].String()] = true
1298+
if p.uses != nil {
1299+
p.uses[toks[0].String()] = true
1300+
}
12971301
if len(toks) == 2 {
12981302
id.Tok2 = toks[1]
1299-
p.uses[toks[1].String()] = true
1303+
if p.uses != nil {
1304+
p.uses[toks[1].String()] = true
1305+
}
13001306
}
13011307
return id
13021308
}
@@ -1807,7 +1813,7 @@ func (p *parser) parseEnum() Expr {
18071813
}
18081814
}
18091815
x := p.parseExpr()
1810-
if id := firstIdent(x); id != nil {
1816+
if id := firstIdent(x); p.names != nil && id != nil {
18111817
p.names[id.String()] = true
18121818
id.IsName = true
18131819
}

ttcn3/syntax/syntax_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestDoc(t *testing.T) {
1111
t.Parallel()
1212

1313
testDoc := func(t *testing.T, input string) string {
14-
root, _, _ := syntax.Parse([]byte(input), syntax.WithFilename(t.Name()))
14+
root := syntax.Parse([]byte(input), syntax.WithFilename(t.Name()))
1515
if err := root.Err(); err != nil {
1616
t.Fatal(err)
1717
}

ttcn3/ttcn3.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func parse(path string, input []byte) *Tree {
7070
input = b
7171
}
7272

73-
root, names, uses := syntax.Parse(input, syntax.WithFilename(path))
73+
names := make(map[string]bool)
74+
uses := make(map[string]bool)
75+
root := syntax.Parse(input, syntax.WithFilename(path), syntax.WithNames(names), syntax.WithUses(uses))
7476
return &Tree{Root: root, Names: names, Uses: uses, Err: root.Err(), filename: path}
7577
}
7678

0 commit comments

Comments
 (0)