fix: multi line triple quoted strings

pull/625/head^2
Nikolaj Sidorenco 2024-05-13 23:35:55 +07:00
parent 7769a19509
commit b5dbafd164
No known key found for this signature in database
8 changed files with 286921 additions and 296111 deletions

@ -1,6 +1,14 @@
namespace test
type A = {
[<IfFalse; CoolProperty(A = 1, Enabled = false); IsTrue>]
A : int
}
module Json =
[<Literal>]
let MyPayload =
"""
{
"prop1": []
"prop2": {
"prop3": true,
"prop4": 1,
},
}
"""

@ -65,7 +65,7 @@ module.exports = grammar({
$._then,
$._else,
$._elif,
$._triple_quoted_end,
$._triple_quoted_content,
$.block_comment_content,
$.line_comment,
@ -1039,7 +1039,7 @@ module.exports = grammar({
_compound_type: $ => prec.right(seq($.type, repeat1(prec.right(seq('*', $.type))))),
_postfix_type: $ => prec.left(4, seq($.type, $.long_identifier)),
_list_type: $ => seq($.type, '[]'),
_static_type: $ => prec(10, seq($.type, $.type_argument_defn)),
_static_type: $ => prec(10, seq($.type, $.type_arguments)),
_constrained_type: $ => prec.right(seq($.type_argument, ':>', $.type)),
_flexible_type: $ => prec.right(seq(token.immediate('#'), $.type)),
@ -1055,6 +1055,7 @@ module.exports = grammar({
// measure
// static-parameter
),
type_attributes: $ => seq($.type_attribute, repeat(prec.left(PREC.COMMA, seq(',', $.type_attribute)))),
atomic_type: $ =>
@ -1653,18 +1654,20 @@ module.exports = grammar({
bytearray: $ => seq('"', repeat($._string_char), token.immediate('"B')),
verbatim_bytearray: $ => seq('@"', repeat($._verbatim_string_char), token.immediate('"B')),
_triple_quoted_end: _ => token.immediate('"""'),
format_triple_quoted_string: $ =>
seq(
token(prec(100, '$"""')),
repeat(choice($.format_string_eval, $._string_char)),
// repeat(choice($.format_string_eval, $._string_char)),
$._triple_quoted_content,
'"""',
),
triple_quoted_string: $ =>
choice(
seq('"""', repeat($._string_char), $._triple_quoted_end),
seq(
'"""',
$._triple_quoted_content,
'"""'),
$.format_triple_quoted_string,
),

38
src/grammar.json generated

@ -3388,7 +3388,7 @@
},
{
"type": "SYMBOL",
"name": "type_argument_defn"
"name": "type_arguments"
}
]
}
@ -6497,13 +6497,6 @@
}
]
},
"_triple_quoted_end": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "\"\"\""
}
},
"format_triple_quoted_string": {
"type": "SEQ",
"members": [
@ -6519,20 +6512,8 @@
}
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "format_string_eval"
},
{
"type": "SYMBOL",
"name": "_string_char"
}
]
}
"type": "SYMBOL",
"name": "_triple_quoted_content"
},
{
"type": "STRING",
@ -6551,15 +6532,12 @@
"value": "\"\"\""
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_string_char"
}
"type": "SYMBOL",
"name": "_triple_quoted_content"
},
{
"type": "SYMBOL",
"name": "_triple_quoted_end"
"type": "STRING",
"value": "\"\"\""
}
]
},
@ -7692,7 +7670,7 @@
},
{
"type": "SYMBOL",
"name": "_triple_quoted_end"
"name": "_triple_quoted_content"
},
{
"type": "SYMBOL",

14
src/node-types.json generated

@ -1497,17 +1497,7 @@
{
"type": "format_triple_quoted_string",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "format_string_eval",
"named": true
}
]
}
"fields": {}
},
{
"type": "fsi_directive_decl",
@ -2931,7 +2921,7 @@
"named": true
},
{
"type": "type_argument_defn",
"type": "type_arguments",
"named": true
},
{

582833
src/parser.c generated

File diff suppressed because it is too large Load Diff

@ -9,7 +9,7 @@ enum TokenType {
THEN,
ELSE,
ELIF,
TRIPLE_QUOTE_END,
TRIPLE_QUOTE_CONTENT,
BLOCK_COMMENT_CONTENT,
LINE_COMMENT,
ERROR_SENTINEL
@ -107,8 +107,29 @@ bool tree_sitter_fsharp_external_scanner_scan(void *payload, TSLexer *lexer,
bool error_recovery_mode = valid_symbols[ERROR_SENTINEL];
if (valid_symbols[TRIPLE_QUOTE_END] && !error_recovery_mode) {
return false;
if (valid_symbols[TRIPLE_QUOTE_CONTENT] && !error_recovery_mode) {
lexer->mark_end(lexer);
while (true) {
if (lexer->lookahead == '\0') {
break;
}
if (lexer->lookahead != '"') {
advance(lexer);
} else {
lexer->mark_end(lexer);
skip(lexer);
if (lexer->lookahead == '"') {
skip(lexer);
if (lexer->lookahead == '"') {
skip(lexer);
break;
}
}
lexer->mark_end(lexer);
}
}
lexer->result_symbol = TRIPLE_QUOTE_CONTENT;
return true;
}
lexer->mark_end(lexer);

@ -167,3 +167,83 @@ let f x = x + 1
(infix_op)
(const
(int))))))
================================================================================
json payload in multi-line string
================================================================================
namespace test
module Json =
[<Literal>]
let MyPayload =
"""
{
"prop1": []
"prop2": {
"prop3": true,
"prop4": 1,
},
}
"""
--------------------------------------------------------------------------------
(file
(namespace
(long_identifier
(identifier))
(module_defn
(identifier)
(value_declaration
(attributes
(attribute_set
(attribute
(object_construction
(type
(long_identifier
(identifier)))))))
(function_or_value_defn
(value_declaration_left
(identifier_pattern
(long_identifier
(identifier))))
(const
(triple_quoted_string)))))))
================================================================================
json payload in multi-line format string
================================================================================
namespace test
module Json =
let myPayloadBuilder x =
$"""
{
"prop1": []
"prop2": {
"prop3": {x},
"prop4": 1,
},
}
"""
--------------------------------------------------------------------------------
(file
(namespace
(long_identifier
(identifier))
(module_defn
(identifier)
(value_declaration
(function_or_value_defn
(function_declaration_left
(identifier)
(argument_patterns
(long_identifier
(identifier))))
(const
(triple_quoted_string
(format_triple_quoted_string))))))))

@ -486,11 +486,4 @@ let x =
(identifier))))
(const
(triple_quoted_string
(format_triple_quoted_string
(format_string_eval
(infix_expression
(const
(int))
(infix_op)
(const
(int))))))))))
(format_triple_quoted_string))))))