@@ -22,13 +22,32 @@ type Mode uint
22
22
23
23
type ParserOption func (* parser ) error
24
24
25
+ // WithFilename sets the filename of the source code.
25
26
func WithFilename (filename string ) ParserOption {
26
27
return func (p * parser ) error {
27
28
p .Filename = filename
28
29
return nil
29
30
}
30
31
}
31
32
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
+
32
51
const (
33
52
PedanticSemicolon = 1 << iota // expect semicolons pedantically
34
53
IgnoreComments // ignore comments
@@ -48,9 +67,6 @@ func NewParser(src []byte) *parser {
48
67
p .ppDefs ["0" ] = false
49
68
p .ppDefs ["1" ] = true
50
69
51
- p .names = make (map [string ]bool )
52
- p .uses = make (map [string ]bool )
53
-
54
70
p .Root = newRoot (src )
55
71
56
72
// fetch first token
@@ -60,24 +76,8 @@ func NewParser(src []byte) *parser {
60
76
return & p
61
77
}
62
78
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 ) {
81
81
82
82
region := trc .StartRegion (context .Background (), "syntax.Parse" )
83
83
defer region .End ()
@@ -86,7 +86,7 @@ func Parse(src []byte, opts ...ParserOption) (root *Root, names map[string]bool,
86
86
for _ , opt := range opts {
87
87
if err := opt (p ); err != nil {
88
88
p .Root .errs = append (p .Root .errs , err )
89
- return p .Root , p . names , p . uses
89
+ return p .Root
90
90
}
91
91
}
92
92
for p .tok != EOF {
@@ -102,7 +102,7 @@ func Parse(src []byte, opts ...ParserOption) (root *Root, names map[string]bool,
102
102
}
103
103
104
104
}
105
- return p .Root , p . names , p . uses
105
+ return p .Root
106
106
}
107
107
108
108
// If src != nil, readSource converts src to a []byte if possible;
@@ -1009,7 +1009,9 @@ func (p *parser) parseName() *Ident {
1009
1009
switch p .tok {
1010
1010
case IDENT , ADDRESS , CONTROL , CLASS :
1011
1011
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
+ }
1013
1015
return id
1014
1016
}
1015
1017
p .expect (IDENT )
@@ -1278,7 +1280,7 @@ func (p *parser) addName(n Node) {
1278
1280
p .addName (n )
1279
1281
}
1280
1282
default :
1281
- if name := Name (n ); name != "" {
1283
+ if name := Name (n ); p . names != nil && name != "" {
1282
1284
p .names [name ] = true
1283
1285
}
1284
1286
}
@@ -1293,10 +1295,14 @@ func (p *parser) make_use(toks ...Token) *Ident {
1293
1295
panic ("No support for multi-token identifiers." )
1294
1296
}
1295
1297
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
+ }
1297
1301
if len (toks ) == 2 {
1298
1302
id .Tok2 = toks [1 ]
1299
- p .uses [toks [1 ].String ()] = true
1303
+ if p .uses != nil {
1304
+ p .uses [toks [1 ].String ()] = true
1305
+ }
1300
1306
}
1301
1307
return id
1302
1308
}
@@ -1807,7 +1813,7 @@ func (p *parser) parseEnum() Expr {
1807
1813
}
1808
1814
}
1809
1815
x := p .parseExpr ()
1810
- if id := firstIdent (x ); id != nil {
1816
+ if id := firstIdent (x ); p . names != nil && id != nil {
1811
1817
p .names [id .String ()] = true
1812
1818
id .IsName = true
1813
1819
}
0 commit comments