Skip to content

Commit f615401

Browse files
committed
Parse block arguments correctly.
The lexer will eat any whitespace or newlines between the block's start symbol and the parameter list, if one is present. Otherwise, the extra NEWLINE tokens will confuse the parser.
1 parent b3c94ba commit f615401

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

examples/input.seg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
|a, b|
2+
|a, b, c, d|
33
3 + 4
44
}

src/grammar.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ expr (OUT) ::= invocation (I). { OUT = I; }
122122

123123
// Blocks
124124

125-
block (OUT) ::= LCURLY parameters (PARAMS) statementlist (BODY) RCURLY.
125+
block (OUT) ::= BLOCKSTART parameters (PARAMS) statementlist (BODY) BLOCKEND.
126126
{
127127
OUT = malloc(sizeof(seg_block_node));
128128
OUT->parameters = PARAMS;
@@ -136,7 +136,7 @@ commaparams (OUT) ::= parameter (IN). { OUT = IN; }
136136
commaparams (OUT) ::= commaparams (LIST) COMMA parameter (NEW).
137137
{
138138
/* Parameters are pushed in reverse order. */
139-
LIST->next = NEW;
139+
NEW->next = LIST;
140140
OUT = NEW;
141141
}
142142

src/lexer.rl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ static void report(const char *name, const char *ts, const char *te) {
8080

8181
'(' => { EMPTY(LPAREN); };
8282
')' => { EMPTY(RPAREN); };
83-
'{' => { EMPTY(LCURLY); };
84-
'}' => { EMPTY(RCURLY); };
8583
';' => { EMPTY(SEMI); };
8684
'\n' => { EMPTY(NEWLINE); };
8785
'=' => { EMPTY(ASSIGNMENT); };
8886
'.' => { EMPTY(PERIOD); };
8987
',' => { EMPTY(COMMA); };
9088

89+
# Eat whitespace and newlines at the start of a block.
90+
# Otherwise, the extraneous NEWLINE tokens will confuse the parser.
91+
'{' (whitespace | [\r\n])* => { EMPTY(BLOCKSTART); };
92+
'}' => { EMPTY(BLOCKEND); };
93+
9194
'|' => {
9295
EMPTY(BAR);
9396
fcall blockargs;

0 commit comments

Comments
 (0)