parse EOL as a token

This commit is contained in:
2026-05-15 00:16:13 -07:00
parent c8ad7e74e7
commit 64e5467062
8 changed files with 16909 additions and 15378 deletions
+30 -9
View File
@@ -68,6 +68,12 @@ export default grammar({
$._block_string_start,
$._block_string_content,
$._block_string_end,
// PICO-8 line-significance: terminates the body of `if (cond) ...` /
// `while (cond) ...` shorthand. The scanner emits this only when the
// parser is at a state expecting it; everywhere else a newline falls
// through to /\s/ in extras and is skipped. See src/scanner.c.
$._line_end,
],
supertypes: ($) => [$.statement, $.expression, $.declaration, $.variable],
@@ -168,14 +174,20 @@ export default grammar({
'end'
),
// PICO-8 single-line: while (cond) stmt
// PICO-8 single-line: while (cond) stmt {stmt}
// Body extends to end-of-line (or EOF). The $._line_end terminator
// is emitted by the external scanner when it sees \n/\r/EOF at a
// position where the parser expects line-end; until then, additional
// statements on the same line accumulate into the body.
shorthand_while_statement: ($) =>
seq(
'while',
'(',
field('condition', $.expression),
')',
field('body', $.statement)
field('body', $.statement),
repeat(field('body', $.statement)),
$._line_end
),
repeat_statement: ($) =>
@@ -205,19 +217,28 @@ export default grammar({
),
else_statement: ($) => seq('else', field('body', optional_block($))),
// PICO-8 single-line: if (cond) stmt [else stmt]
// prec.right resolves the dangling-else ambiguity in favor of greedy
// attach to the nearest preceding shorthand `if`, matching PICO-8
// semantics where shorthand if/else live on one line.
// PICO-8 single-line: if (cond) stmt {stmt} [else stmt {stmt}]
// Both the consequence and the alternative extend to end-of-line.
// The $._line_end terminator (emitted by the external scanner on
// \n/\r/EOF) prevents a later-line `else` from binding to a
// shorthand `if` on a previous line, matching PICO-8 semantics.
shorthand_if_statement: ($) =>
prec.right(seq(
seq(
'if',
'(',
field('condition', $.expression),
')',
field('consequence', $.statement),
optional(seq('else', field('alternative', $.statement)))
)),
repeat(field('consequence', $.statement)),
optional(
seq(
'else',
field('alternative', $.statement),
repeat(field('alternative', $.statement))
)
),
$._line_end
),
for_statement: ($) =>
seq(