1
1
from pyparsing import (
2
2
Forward ,
3
3
Suppress ,
4
+ Combine ,
4
5
Group ,
5
6
Opt ,
6
7
Empty ,
10
11
11
12
from pynescript import ast
12
13
from pynescript .ast .parser .parser_elements import (
14
+ ResultNameableForward as Forward ,
15
+ IndentedBlockWithTabs as IndentedBlock ,
13
16
ConvertToNode ,
14
17
)
15
18
from pynescript .ast .parser .tokens import (
40
43
expression = Forward ()
41
44
local_statement = Forward ()
42
45
46
+ type_specifier = Forward ()
47
+ type_specifier .set_name ("type_specifier" )
48
+
43
49
atomic_type_name = (
44
50
ConvertToNode (ast .Int )(INT )
45
51
| ConvertToNode (ast .Float )(FLOAT )
53
59
| ConvertToNode (ast .Table )(TABLE )
54
60
)
55
61
62
+ atomic_type_name .set_name ("atomic_type_name" )
63
+
56
64
array_type_name = ConvertToNode (ast .Array )(ARRAY )
65
+ array_type_name .set_name ("array_type_name" )
66
+
57
67
matrix_type_name = ConvertToNode (ast .Matrix )(MATRIX )
68
+ matrix_type_name .set_name ("matrix_type_name" )
58
69
59
70
collection_type_name = array_type_name | matrix_type_name
71
+ collection_type_name .set_name ("collection_type_name" )
72
+
73
+ type_name = collection_type_name | atomic_type_name
74
+ type_name .set_name ("type_name" )
75
+
76
+ type_argument = Suppress (LCHEVRON ) + type_specifier + Suppress (RCHEVRON )
77
+ type_argument .set_name ("type_argument" )
78
+
79
+ collection_type_specifier = ConvertToNode (ast .CollectionType )(
80
+ collection_type_name ("type_name" ) + type_argument ("type_argument" )
81
+ )
82
+ collection_type_specifier .set_name ("collection_type_specifier" )
83
+
84
+ array_type_suffix = Combine (LBRACKET + RBRACKET )
85
+ array_type_suffix .set_name ("array_type_suffix" )
86
+
87
+ array_type_specifier = ConvertToNode (ast .ArrayType )(
88
+ Combine (type_specifier ("element_type" ) + Suppress (array_type_suffix ))
89
+ )
90
+ array_type_specifier .set_name ("array_type_specifier" )
91
+
92
+ atomic_type_specifier = atomic_type_name
93
+ atomic_type_specifier .set_name ("atomic_type_specifier" )
94
+
95
+ # this gives "ParseException raised: Forward recursion without base case, found '?' ..." error
96
+ type_specifier <<= (
97
+ collection_type_specifier | array_type_specifier | atomic_type_specifier
98
+ )
99
+
100
+ # this solves the recursion problem above, but requires extra effort on parser action
101
+ type_specifier_trailing = Forward ()
102
+ type_specifier_trailing <<= Opt (
103
+ (array_type_suffix | type_argument ) + type_specifier_trailing
104
+ )
105
+
106
+
107
+ def handle_type_specifier (results ):
108
+ last_token = results [- 1 ]
109
+ if len (results ) == 1 :
110
+ return last_token
111
+ else :
112
+ results_except_last_token = results [:- 1 ]
113
+ if last_token == "[]" :
114
+ element_type_token = handle_type_specifier (results_except_last_token )
115
+ return ast .ArrayType (element_type_token )
116
+ else :
117
+ type_name_token = handle_type_specifier (results_except_last_token )
118
+ type_argument_token = last_token
119
+ return ast .CollectionType (type_name_token , type_argument_token )
120
+
121
+
122
+ type_specifier <<= ConvertToNode (handle_type_specifier )(
123
+ Group (type_name + type_specifier_trailing )
124
+ )
60
125
61
- type_argument = Suppress (LCHEVRON ) + atomic_type_name + Suppress (RCHEVRON )
62
126
63
127
name = ConvertToNode (ast .Name )(IDENTIFIER )
64
- attributed_name = Empty () + delimited_list (name , DOT ).leave_whitespace ()
65
128
66
129
67
- @attributed_name .set_parse_action
68
130
def handle_attributed_name (results ):
69
131
node = results [0 ]
70
132
for attr in results [1 :]:
@@ -82,6 +144,11 @@ def handle_attributed_name(results):
82
144
return node
83
145
84
146
147
+ attributed_name = ConvertToNode (handle_attributed_name )(
148
+ Group (Empty () + delimited_list (name , DOT ).leave_whitespace ())
149
+ )
150
+
151
+
85
152
full_argument = IDENTIFIER ("name" ) + Suppress (ASSIGN ) + expression ("value" )
86
153
value_only_argument = expression ("value" )
87
154
@@ -91,7 +158,7 @@ def handle_attributed_name(results):
91
158
92
159
function_call = ConvertToNode (ast .Call )(
93
160
attributed_name ("name" )
94
- + Opt (type_argument )
161
+ + Opt (type_argument ( "type_argument" ) )
95
162
+ Suppress (LPAREN )
96
163
+ Opt (argument_list ("arguments" ))
97
164
+ Suppress (RPAREN )
0 commit comments