Skip to content

Commit

Permalink
Change comment starter, add commas and semicolons as separators
Browse files Browse the repository at this point in the history
  • Loading branch information
treuherz committed Apr 6, 2024
1 parent 6fb1953 commit e220cd0
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
## 2024-04-06

* Stopped the page crashing if input was unparseable, added an indicator for parse success.
* Change the comment-starter to //
* Add commas and semicolons as record and series separators
7 changes: 4 additions & 3 deletions rampart-web/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function App() {
</p>
</section>
<section className="help">
<p>Each line is a time/power pair. Blank lines separate series.</p>
<p>Each pair of values is a time and a power pair, separated by a newline or a comma.
Blank lines or semicolons separate series.</p>

<p>
Plain integer times are minutes, other valid formats are{" "}
Expand All @@ -81,8 +82,8 @@ function App() {
</p>

<p>
Comments can be added by starting them with a semicolon,{" "}
<code>; like this.</code>
Comments start with a double forward-slash,{" "}
<code>// like this.</code>
</p>
</section>
<section className="chart">
Expand Down
11 changes: 6 additions & 5 deletions tree-sitter-rampart/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ module.exports = grammar({

_newline: $ => '\n',

_terminator: $ => choice('\n', '\0'),
_record_sep: $ => seq(choice('\n', ',', ',\n'), repeat(' ')),
_series_sep: $ => seq(choice('\n', '\0', ';', ';\n', ';\0'), repeat(' '), repeat('\n')),

// Series consist of optional attributes and at least one point and end with at lease one newline.
series: $ => seq(
field('attributes', optional($.attribute_list)),
field('points', $.point_list),
$._terminator,
$._series_sep,
),

// Attributes are a list of '@key value' pairs
attribute_list: $ => repeat1(seq($.attribute, $._newline)),
attribute_list: $ => repeat1(seq($.attribute, $._record_sep)),

// @key value
// @key "v a l u e"
Expand All @@ -42,7 +43,7 @@ module.exports = grammar({
qstring: $ => /[ a-zA-Z0-9._'-]+/,

// Point lists are lists of time, power points
point_list: $ => repeat1(seq($.point, $._newline)),
point_list: $ => prec.right(seq(repeat(seq($.point, $._record_sep)), $.point, optional($._record_sep))),

// Times and powers are separated by a space
point: $ => seq(
Expand Down Expand Up @@ -84,7 +85,7 @@ module.exports = grammar({
// 1.23456e2
exponent: $ => /-?(\d(_?\d)*)?(\.(_?\d)*)?[eE]-?\d(_?\d)*/,

comment: $ => /\s*;.*/,
comment: $ => /\s*\/\/.*/,
},

// Comments are ignored
Expand Down
109 changes: 97 additions & 12 deletions tree-sitter-rampart/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,76 @@
"type": "STRING",
"value": "\n"
},
"_terminator": {
"type": "CHOICE",
"_record_sep": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\n"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\n"
},
{
"type": "STRING",
"value": ","
},
{
"type": "STRING",
"value": ",\n"
}
]
},
{
"type": "STRING",
"value": "\u0000"
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
}
]
},
"_series_sep": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\n"
},
{
"type": "STRING",
"value": "\u0000"
},
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": ";\n"
},
{
"type": "STRING",
"value": ";\u0000"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": "\n"
}
}
]
},
Expand Down Expand Up @@ -66,7 +126,7 @@
},
{
"type": "SYMBOL",
"name": "_terminator"
"name": "_series_sep"
}
]
},
Expand All @@ -81,7 +141,7 @@
},
{
"type": "SYMBOL",
"name": "_newline"
"name": "_record_sep"
}
]
}
Expand Down Expand Up @@ -169,17 +229,42 @@
"value": "[ a-zA-Z0-9._'-]+"
},
"point_list": {
"type": "REPEAT1",
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "point"
},
{
"type": "SYMBOL",
"name": "_record_sep"
}
]
}
},
{
"type": "SYMBOL",
"name": "point"
},
{
"type": "SYMBOL",
"name": "_newline"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_record_sep"
},
{
"type": "BLANK"
}
]
}
]
}
Expand Down Expand Up @@ -380,7 +465,7 @@
},
"comment": {
"type": "PATTERN",
"value": "\\s*;.*"
"value": "\\s*\\/\\/.*"
}
},
"extras": [
Expand Down
20 changes: 20 additions & 0 deletions tree-sitter-rampart/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@
"type": "\"",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": ",\n",
"named": false
},
{
"type": ";",
"named": false
},
{
"type": ";\u0000",
"named": false
},
{
"type": ";\n",
"named": false
},
{
"type": "@",
"named": false
Expand Down
Binary file modified tree-sitter-rampart/src/parser.c
Binary file not shown.
Loading

0 comments on commit e220cd0

Please sign in to comment.