Skip to content

Commit

Permalink
Bugfix for multiline values
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddytrombone committed Oct 20, 2023
1 parent c90b8a1 commit f449148
Show file tree
Hide file tree
Showing 6 changed files with 1,400 additions and 1,287 deletions.
17 changes: 17 additions & 0 deletions corpus/assignment.txtt
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,20 @@ foobar
(identifier)
(multiline_value
(constant))))


====
Assignment with multiline value and parentheses inline
====

foo (
bar (baz
foobar)
test
)

---
(typoscript
(multiline_line
(identifier)
(multiline_value)))
54 changes: 30 additions & 24 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
function toCaseInsensitive(a) {
var ca = a.charCodeAt(0);
if (ca >= 97 && ca <= 122) return `[${a}${a.toUpperCase()}]`;
if (ca >= 65 && ca <= 90) return `[${a.toLowerCase()}${a}]`;
return a;
}

function caseInsensitive(keyword) {
return new RegExp(keyword
.split('')
.map(toCaseInsensitive)
.join('')
)
}

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

Expand Down Expand Up @@ -42,7 +27,7 @@ module.exports = grammar({

assignment_line: $ => seq($.identifier, '=', optional(choice($.cobject, $.value)), '\n'),

multiline_line: $ => seq($.identifier, $.multiline_value, '\n'),
multiline_line: $ => seq($.identifier, $.multiline_value, optional($._comments), '\n'),

deletion_line: $ => seq($.identifier, '>', optional($._comments), '\n'),

Expand Down Expand Up @@ -76,7 +61,7 @@ module.exports = grammar({

value: $ => repeat1(choice(/[^\n]/, $.constant)),

multiline_value: $ => stringWithConstants($, '(', ')', ')', true),
multiline_value: $ => seq('(', repeat(seq(optional(repeat1(choice($.constant, /[^\n]/))), '\n')), ')'),

identifier: $ => /((?:\.)|(?:[a-zA-Z0-9_\-\\]+(?:\.[a-zA-Z0-9_\-\\]*)*))/,

Expand All @@ -97,8 +82,8 @@ module.exports = grammar({
single_line_comment: $ => token(seq(choice("#", "//"), /.*/)),

string: $ => choice(
stringWithConstants($, '"'),
stringWithConstants($, '\'')
stringWithConstantsSeq($, '"'),
stringWithConstantsSeq($, '\'')
),

_imports: $ => choice($.import_legacy, $.import),
Expand All @@ -109,17 +94,22 @@ module.exports = grammar({
}
});

function stringWithConstants($, startString, endString, endStringForRegex) {
function repeatedStringWithConstants($, stringForRegex, dontEscape) {
var regex = '[^' + (dontEscape === true ? '' : '\\') + stringForRegex + ']';
return optional(repeat1(choice(
$.constant,
new RegExp(regex)
)));
}

function stringWithConstantsSeq($, startString, endString, endStringForRegex) {
if (typeof (endString) === 'undefined') {
endString = startString;
}
if (typeof (endStringForRegex) === 'undefined') {
endStringForRegex = endString;
}
return seq(startString, optional(repeat1(choice(
$.constant,
new RegExp('[^\\' + endStringForRegex + ']')
))), endString);
return seq(startString, repeatedStringWithConstants($, endStringForRegex), endString);
}

function sep(separator, rule) {
Expand All @@ -128,4 +118,20 @@ function sep(separator, rule) {

function sep1(separator, rule) {
return seq(rule, repeat(seq(separator, rule)));

}
function toCaseInsensitive(a) {
var ca = a.charCodeAt(0);
if (ca >= 97 && ca <= 122) return `[${a}${a.toUpperCase()}]`;
if (ca >= 65 && ca <= 90) return `[${a.toLowerCase()}${a}]`;
return a;
}

function caseInsensitive(keyword) {
return new RegExp(keyword
.split('')
.map(toCaseInsensitive)
.join('')
)
}

52 changes: 38 additions & 14 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@
"type": "SYMBOL",
"name": "multiline_value"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_comments"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "\n"
Expand Down Expand Up @@ -540,28 +552,40 @@
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "constant"
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "constant"
},
{
"type": "PATTERN",
"value": "[^\\n]"
}
]
}
},
{
"type": "PATTERN",
"value": "[^\\)]"
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "\n"
}
},
{
"type": "BLANK"
}
]
]
}
},
{
"type": "STRING",
Expand Down
8 changes: 8 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,21 @@
"multiple": true,
"required": true,
"types": [
{
"type": "comment",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "multiline_value",
"named": true
},
{
"type": "single_line_comment",
"named": true
}
]
}
Expand Down
Loading

0 comments on commit f449148

Please sign in to comment.