Skip to content

Commit

Permalink
Grammar: Fix a bunch of issues related to symbol fragmentation and al…
Browse files Browse the repository at this point in the history
…lowed format specifiers (#14)

Related to open-goal/jak-project#3535
  • Loading branch information
xTVaser authored May 25, 2024
1 parent 7ff208a commit 2d148cb
Show file tree
Hide file tree
Showing 8 changed files with 1,980 additions and 1,483 deletions.
17 changes: 11 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ const INTEGER =

// TODO - does OG support negative hex/binary?
const NUMBER =
token(prec(10, seq(optional(/[+-]/),
token(seq(optional(/[+-]/),
choice(HEX_NUMBER,
BINARY_NUMBER,
FLOAT,
INTEGER))));
INTEGER)));

const NULL =
token('none');
Expand Down Expand Up @@ -117,16 +117,19 @@ const CHARACTER =
// \u205f => <medium mathematical space>
// \u3000 => <ideographic space>
const SYMBOL_HEAD =
/[^\f\n\r\t \/()\[\]{}"@~^;`\\,:#'0-9\u000B\u001C\u001D\u001E\u001F\u2028\u2029\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2008\u2009\u200a\u205f\u3000]/;
/[^\f\n\r\t ()\[\]{}"@~^;`\\,:'0-9\u000B\u001C\u001D\u001E\u001F\u2028\u2029\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2008\u2009\u200a\u205f\u3000]/;

const SYMBOL_BODY =
choice(SYMBOL_HEAD,
/[:#'0-9]/);
/[:#~'0-9\/]/);

const SYMBOL =
token(seq(SYMBOL_HEAD,
repeat(SYMBOL_BODY)));

// Define a rule specifically for symbols starting with digits followed by non-digit characters
const DIGIT_SYMBOL = token(seq(repeat1(DIGIT), SYMBOL_BODY, repeat(SYMBOL_BODY)));

module.exports = grammar({
name: 'opengoal',

Expand Down Expand Up @@ -205,7 +208,7 @@ module.exports = grammar({
seq(field('numberOfArgs', $._format_token), '*'),
'?',
"Newline",
seq(repeat(choice($._format_token, ',')), /[$mrRbBdDgGxXeEoOsStTfHhJjKkLlNnVwWyYzZ]/),
seq(repeat(choice($._format_token, ',')), /[$AmMrRbBdDgGxXeEoOsStTfFHhJjKkLlNnVwWyYzZ]/),
),
format_specifier: $ =>
prec.left(seq(
Expand Down Expand Up @@ -237,7 +240,9 @@ module.exports = grammar({
BOOLEAN,

sym_lit: $ =>
seq(choice($._sym_unqualified)),
seq(choice($._sym_unqualified, $._digit_sym)),

_digit_sym: $ => field('name', alias(DIGIT_SYMBOL, $.sym_name)),

_sym_unqualified: $ =>
field('name', alias(choice("/", SYMBOL),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "bindings/node",
"scripts": {
"gen": "yarn tree-sitter generate",
"test": "yarn tree-sitter test",
"test": "yarn tree-sitter generate && yarn tree-sitter test",
"parse-sample": "yarn gen && yarn tree-sitter parse test/samples/sample.gc",
"parse-test": "yarn gen && yarn tree-sitter parse test/samples/test.gc",
"install": "node-gyp-build",
Expand Down
Loading

0 comments on commit 2d148cb

Please sign in to comment.