@@ -9,46 +9,79 @@ import {
99} from "../print_utils" ;
1010import { CstToDocMap } from "../CstToDocMap" ;
1111
12- export const proceduralLanguageMap : Partial < CstToDocMap < AllProceduralNodes > > = {
12+ export const proceduralLanguageMap : CstToDocMap < AllProceduralNodes > = {
13+ // labels
14+ labeled_stmt : ( print , node ) =>
15+ group ( [
16+ print . spaced ( [ "beginLabel" , "statement" ] ) ,
17+ node . endLabel ? [ " " , print ( [ "endLabel" ] ) ] : [ ] ,
18+ ] ) ,
19+ colon_label : ( print ) => [ print ( "label" ) , ":" ] ,
20+ chevron_label : ( print ) => [ "<<" , print ( "label" ) , ">>" ] ,
21+
1322 // BEGIN .. END
1423 block_stmt : ( print , node ) =>
1524 group ( [
16- print . spaced ( [ "beginKw" , "atomicKw" ] ) ,
17- indent ( [ hardline , stripTrailingHardline ( print ( "program" ) ) ] ) ,
25+ stripTrailingHardline ( print ( "declareClause" ) ) ,
26+ [
27+ node . declareClause ? hardline : [ ] ,
28+ print . spaced ( [ "beginKw" , "atomicKw" ] ) ,
29+ ] ,
30+ node . program . statements . length > 0
31+ ? indent ( [ hardline , stripTrailingHardline ( print ( "program" ) ) ] )
32+ : print ( "program" ) ,
1833 node . exception ? [ hardline , print ( "exception" ) ] : [ ] ,
1934 hardline ,
2035 print ( "endKw" ) ,
2136 ] ) ,
37+
38+ // DECLARE
39+ declare_clause : ( print , node ) =>
40+ group ( [ print ( "declareKw" ) , indent ( [ hardline , print ( "program" ) ] ) ] ) ,
41+ declare_stmt : ( print ) =>
42+ group (
43+ join ( " " , [
44+ ...print . spaced ( [ "declareKw" ] ) ,
45+ group ( print ( "names" ) ) ,
46+ ...print . spaced ( [ "constantKw" , "dataType" , "constraints" , "init" ] ) ,
47+ ] ) ,
48+ ) ,
49+ declare_init : ( print ) => print . spaced ( [ "operator" , "expr" ] ) ,
50+
51+ // EXCEPTION
2252 exception_clause : ( print , node ) => {
2353 if ( node . clauses . length === 1 ) {
2454 // Keep single exception clause on the same line as EXCEPTION keyword
2555 return group ( print . spaced ( [ "exceptionKw" , "clauses" ] ) ) ;
2656 } else {
27- return group ( [ print ( "exceptionKw" ) , indent ( [ line , print ( "clauses" ) ] ) ] ) ;
57+ return group ( [
58+ print ( "exceptionKw" ) ,
59+ indent ( [
60+ hardline ,
61+ stripTrailingHardline ( print ( "clauses" ) . map ( ( doc ) => [ doc , hardline ] ) ) ,
62+ ] ) ,
63+ ] ) ;
2864 }
2965 } ,
30- exception_when_clause : ( print ) =>
66+ exception_when_clause : ( print , node ) =>
3167 group ( [
32- print . spaced ( [ "whenKw" , "condition" , "thenKw" ] ) ,
33- indent ( [ hardline , stripTrailingHardline ( print ( "program" ) ) ] ) ,
68+ join ( " " , [ print ( "whenKw" ) , group ( print ( "condition" ) ) , print ( "thenKw" ) ] ) ,
69+ node . program . statements . length > 0
70+ ? indent ( [ hardline , stripTrailingHardline ( print ( "program" ) ) ] )
71+ : print ( "program" ) ,
3472 ] ) ,
3573
3674 error_bigquery : ( print ) => print ( "errorKw" ) ,
37-
38- // DECLARE
39- declare_stmt : ( print ) =>
40- group (
41- join ( " " , [
42- print ( "declareKw" ) ,
43- group ( print ( "names" ) ) ,
44- ...print . spaced ( [ "dataType" , "init" ] ) ,
45- ] ) ,
46- ) ,
47- declare_init : ( print ) => print . spaced ( [ "operator" , "expr" ] ) ,
48-
75+ error_name : ( print ) => print ( "name" ) ,
76+ error_sqlstate : ( print ) => group ( print . spaced ( [ "sqlstateKw" , "code" ] ) ) ,
77+ error_format_string : ( print , node ) => group ( print ( [ "format" , "args" ] ) ) ,
4978 // SET
5079 set_stmt : ( print ) => group ( print . spaced ( [ "setKw" , "assignments" ] ) ) ,
5180
81+ // assignment
82+ assignment_stmt : ( print ) =>
83+ group ( print . spaced ( [ "target" , "operator" , "expr" ] ) ) ,
84+
5285 // IF
5386 if_stmt : ( print ) =>
5487 group ( join ( hardline , [ ...print ( "clauses" ) , print . spaced ( "endIfKw" ) ] ) ) ,
@@ -108,6 +141,9 @@ export const proceduralLanguageMap: Partial<CstToDocMap<AllProceduralNodes>> = {
108141 hardline ,
109142 print . spaced ( "endWhileKw" ) ,
110143 ] ) ,
144+ // WHILE .. LOOP .. END LOOP
145+ while_loop_stmt : ( print ) =>
146+ group ( print . spaced ( [ "whileKw" , "condition" , "loop" ] ) ) ,
111147 // FOR .. IN
112148 for_stmt : ( print ) =>
113149 group ( [
@@ -116,26 +152,60 @@ export const proceduralLanguageMap: Partial<CstToDocMap<AllProceduralNodes>> = {
116152 hardline ,
117153 print . spaced ( "endForKw" ) ,
118154 ] ) ,
119- // BREAK/CONTINUE
120- break_stmt : ( print ) => group ( print . spaced ( [ "breakKw" , "label" ] ) ) ,
121- continue_stmt : ( print ) => group ( print . spaced ( [ "continueKw" , "label" ] ) ) ,
122- // labels
123- labeled_stmt : ( print , node ) =>
155+ // FOR .. IN range LOOP .. END LOOP
156+ for_loop_stmt : ( print ) =>
157+ group ( print . spaced ( [ "forKw" , "left" , "inKw" , "right" , "loop" ] ) ) ,
158+ for_range : ( print ) =>
124159 group ( [
125- print . spaced ( [ "beginLabel" , "statement" ] ) ,
126- node . endLabel ? [ " " , print ( [ "endLabel" ] ) ] : [ ] ,
160+ print . spaced ( [ "reverseKw" , "from" ] ) ,
161+ ".." ,
162+ print . spaced ( [ "to" , "by" ] ) ,
127163 ] ) ,
128- colon_label : ( print ) => [ print ( "label" ) , ":" ] ,
164+ for_by_clause : ( print ) => group ( print . spaced ( [ "byKw" , "expr" ] ) ) ,
165+ // FOREACH
166+ foreach_stmt : ( print ) =>
167+ group ( [
168+ print . spaced ( [
169+ "foreachKw" ,
170+ "left" ,
171+ "slice" ,
172+ "inArrayKw" ,
173+ "right" ,
174+ "loop" ,
175+ ] ) ,
176+ ] ) ,
177+ foreach_slice : ( print ) => group ( print . spaced ( [ "sliceKw" , "count" ] ) ) ,
178+ // BREAK/CONTINUE
179+ break_stmt : ( print ) => group ( print . spaced ( [ "breakKw" , "label" , "when" ] ) ) ,
180+ continue_stmt : ( print ) =>
181+ group ( print . spaced ( [ "continueKw" , "label" , "when" ] ) ) ,
129182
130183 // CALL
131184 call_stmt : ( print ) => group ( print . spaced ( [ "callKw" , "func" ] ) ) ,
132185 // RETURN
133186 return_stmt : ( print ) => group ( print . spaced ( [ "returnKw" , "expr" ] ) ) ,
187+ return_next_stmt : ( print ) => group ( print . spaced ( [ "returnNextKw" , "expr" ] ) ) ,
188+ return_query_stmt : ( print , node ) => {
189+ if ( node . expr . type === "execute_expr" ) {
190+ return group ( [ print . spaced ( "returnQueryKw" ) , " " , print ( "expr" ) ] ) ;
191+ } else {
192+ return group ( [ print . spaced ( "returnQueryKw" ) , " " , indent ( print ( "expr" ) ) ] ) ;
193+ }
194+ } ,
134195 // RAISE
135- raise_stmt : ( print ) => group ( print . spaced ( [ "raiseKw" , "using" ] ) ) ,
136- raise_using_clause : ( print ) => group ( print . spaced ( [ "usingKw" , "options" ] ) ) ,
137- raise_option_element : ( print ) => [ print ( "nameKw" ) , " = " , print ( "value" ) ] ,
196+ raise_stmt : ( print , node ) =>
197+ group ( [
198+ print . spaced ( [ "raiseKw" , "level" , "error" ] ) ,
199+ node . using ? indent ( [ line , print ( "using" ) ] ) : [ ] ,
200+ ] ) ,
201+ raise_level : ( print ) => print ( "levelKw" ) ,
202+ raise_using_clause : ( print ) =>
203+ group ( [ print ( "usingKw" ) , indent ( [ line , print ( "options" ) ] ) ] ) ,
204+ raise_option_element : ( print ) =>
205+ group ( print . spaced ( [ "nameKw" , "operator" , "value" ] ) ) ,
138206 // ASSERT
139207 assert_stmt : ( print ) =>
140208 group ( print . spaced ( [ "assertKw" , "condition" , "message" ] ) ) ,
209+ // NULL
210+ null_stmt : ( print ) => group ( print . spaced ( [ "nullKw" ] ) ) ,
141211} ;
0 commit comments