-
Hi, I am currently trying to write a grammar for a scripting language that was developed where I work in the 80's, and I'm struggling with the fact that the language accepts whitespace separated floats in a number of contexts (e.g. returning several floats from a procedure). seq($._expression, repeat(seq(' ', $._expression)))) This works fine for lists of floats, but this seems to be causing problems with other stuff, like Is there any chance I can get something like that working or is this kind of grammar too weird to be treated correctly by tree-sitter? grammar.js
module.exports = grammar({
name: 'gibiane',
rules: {
source_file: $ => repeat($.statement),
statement: $ => seq(
repeat1($.identifier),
'=',
seq(
$._expression,
repeat(
seq(
' ',
$._expression,
)
),
),
';',
),
_expression: $ => choice(
$.identifier,
$.integer,
$.float,
$.binary_expression,
),
identifier: $ => /[a-zA-Z_]+/,
integer: $ => /\d+/,
float: $ => {
const digits = /\d+/;
const exponent = seq(/[eEdD][\+-]?/, digits);
return choice(
seq(digits, '.', optional(digits), optional(exponent)),
seq(optional(digits), '.', digits, optional(exponent)),
seq(digits, exponent),
)
},
binary_expression: $ => prec.left(
seq(
field('left', $._expression),
field('operator', '+'),
field('right', $._expression),
),
),
}
}); Debug
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I think there are some grammars that explicitly handle whitespace. May be you already know, but FWIW, the docs say the following about
That's from this section -- may need to scroll down a bit. I did:
across some locally fetched repositories and found there were some that had: extras: $ => [], IIUC, there's a good chance some (or most or all?) might give some hints as to what might be involved. I also explicitly handled whitespace in tree-sitter-clojure, but since that's a lisp-like language, the kinds of things it has to cope with are far tamer than what many other languages face. |
Beta Was this translation helpful? Give feedback.
I'm no expert and also felt similarly. I did manage to write one at one point though.
There is some detailed documentation here and I'll include a list sorted by line count of existing scanners below (the paths are relative to the
repos
directory of ts-questions) [1]. May be looking at the ones with smaller line counts first while going over the docs might help with forming appropriate mental models.I've seen discussions here with folks getting help and there's a discord server for tree-sitter discussions if you don't mind using that sort of thing.