Extract parts of a string as separate AST nodes (#13)

pull/204/head
Jonathan Arnett 2022-02-14 11:35:13 +07:00 committed by GitHub
parent b848d3e234
commit c4743d7776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 19488 additions and 18189 deletions

@ -3,6 +3,7 @@ Constants
================================================================================
const a = "hello"
const a = "hello\nworld\!"
const a = 1_234
const a = 0b110
const a = 0o7224
@ -30,7 +31,14 @@ const a: option.Option(String) = option.Some("Hello, World!")
(source_file
(constant
name: (identifier)
value: (string))
value: (string
(quoted_content)))
(constant
name: (identifier)
value: (string
(quoted_content)
(escape_sequence)
(quoted_content)))
(constant
name: (identifier)
value: (integer))
@ -62,7 +70,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
name: (type_identifier)))
value: (tuple
(integer)
(string)))
(string
(quoted_content))))
(constant
name: (identifier)
type: (tuple_type)
@ -125,7 +134,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
name: (identifier)
value: (bit_string
(bit_string_segment
value: (string)
value: (string
(quoted_content))
options: (bit_string_segment_options
(bit_string_segment_option_utf8)))))
(constant
@ -143,7 +153,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
name: (type_identifier)
arguments: (arguments
(argument
value: (string))
value: (string
(quoted_content)))
(argument
value: (integer)))))
(constant
@ -153,7 +164,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
arguments: (arguments
(argument
label: (label)
value: (string))
value: (string
(quoted_content)))
(argument
label: (label)
value: (integer)))))
@ -166,7 +178,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
arguments: (arguments
(argument
label: (label)
value: (string)))))
value: (string
(quoted_content))))))
(constant
name: (identifier)
type: (type
@ -183,7 +196,8 @@ const a: option.Option(String) = option.Some("Hello, World!")
name: (type_identifier))
arguments: (arguments
(argument
value: (string))))))
value: (string
(quoted_content)))))))
================================================================================
Public constants
@ -212,7 +226,8 @@ pub const a = uri.Uri(host: "github.com")
(source_file
(public_constant
name: (identifier)
value: (string))
value: (string
(quoted_content)))
(public_constant
name: (identifier)
type: (type
@ -232,7 +247,8 @@ pub const a = uri.Uri(host: "github.com")
name: (type_identifier)))
value: (tuple
(integer)
(string)))
(string
(quoted_content))))
(public_constant
name: (identifier)
type: (tuple_type)
@ -295,7 +311,8 @@ pub const a = uri.Uri(host: "github.com")
name: (identifier)
value: (bit_string
(bit_string_segment
value: (string)
value: (string
(quoted_content))
options: (bit_string_segment_options
(bit_string_segment_option_utf8)))))
(public_constant
@ -313,7 +330,8 @@ pub const a = uri.Uri(host: "github.com")
name: (type_identifier)
arguments: (arguments
(argument
value: (string))
value: (string
(quoted_content)))
(argument
value: (integer)))))
(public_constant
@ -323,7 +341,8 @@ pub const a = uri.Uri(host: "github.com")
arguments: (arguments
(argument
label: (label)
value: (string))
value: (string
(quoted_content)))
(argument
label: (label)
value: (integer)))))
@ -336,4 +355,5 @@ pub const a = uri.Uri(host: "github.com")
arguments: (arguments
(argument
label: (label)
value: (string))))))
value: (string
(quoted_content)))))))

@ -31,7 +31,8 @@ pub fn main() {
(label))
(arguments
(argument
(string)))))
(string
(quoted_content))))))
(case_clause
(case_clause_patterns
(case_clause_pattern
@ -44,4 +45,5 @@ pub fn main() {
(label))
(arguments
(argument
(string))))))))))
(string
(quoted_content)))))))))))

@ -26,8 +26,10 @@ external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
(type
name: (type_identifier)))))
body: (external_function_body
(string)
(string)))
(string
(quoted_content))
(string
(quoted_content))))
(external_function
name: (identifier)
parameters: (function_parameters
@ -38,8 +40,10 @@ external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
return_type: (type
name: (type_identifier))
body: (external_function_body
(string)
(string)))
(string
(quoted_content))
(string
(quoted_content))))
(external_function
name: (identifier)
parameters: (function_parameters)
@ -57,8 +61,10 @@ external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
return_type: (type
name: (type_identifier))))
body: (external_function_body
(string)
(string))))
(string
(quoted_content))
(string
(quoted_content)))))
==========================
Public external functions
@ -88,8 +94,10 @@ pub external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
(type
name: (type_identifier)))))
body: (external_function_body
(string)
(string)))
(string
(quoted_content))
(string
(quoted_content))))
(public_external_function
name: (identifier)
parameters: (function_parameters
@ -100,8 +108,10 @@ pub external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
return_type: (type
name: (type_identifier))
body: (external_function_body
(string)
(string)))
(string
(quoted_content))
(string
(quoted_content))))
(public_external_function
name: (identifier)
parameters: (function_parameters)
@ -119,5 +129,7 @@ pub external fn a() -> #(List(Int), fn(Int) -> String) = "x" "y"
return_type: (type
name: (type_identifier))))
body: (external_function_body
(string)
(string))))
(string
(quoted_content))
(string
(quoted_content)))))

@ -294,7 +294,8 @@ fn field_access(x) {
name: (identifier)
parameters: (function_parameters)
body: (function_body
(string)))
(string
(quoted_content))))
(function
name: (identifier)
parameters: (function_parameters)
@ -314,7 +315,8 @@ fn field_access(x) {
arguments: (arguments
(argument
label: (label)
value: (string))))))
value: (string
(quoted_content)))))))
(function
name: (identifier)
parameters: (function_parameters)
@ -326,7 +328,8 @@ fn field_access(x) {
arguments: (arguments
(argument
label: (label)
value: (string))))))
value: (string
(quoted_content)))))))
(function
name: (identifier)
parameters: (function_parameters
@ -346,7 +349,8 @@ fn field_access(x) {
parameters: (function_parameters)
body: (function_body
(todo
message: (string))))
message: (string
(quoted_content)))))
(function
name: (identifier)
parameters: (function_parameters
@ -408,7 +412,8 @@ fn field_access(x) {
(binary_expression
left: (integer)
right: (integer))
(string))))
(string
(quoted_content)))))
(function
name: (identifier)
parameters: (function_parameters
@ -448,7 +453,8 @@ fn field_access(x) {
(binary_expression
left: (integer)
right: (integer))
(string)))
(string
(quoted_content))))
(let
pattern: (identifier)
value: (identifier))
@ -469,7 +475,8 @@ fn field_access(x) {
(binary_expression
left: (integer)
right: (integer))
(string)))
(string
(quoted_content))))
(assert
pattern: (identifier)
value: (identifier))
@ -492,7 +499,8 @@ fn field_access(x) {
arguments: (record_update_arguments
(record_update_argument
label: (label)
value: (string))
value: (string
(quoted_content)))
(record_update_argument
label: (label)
value: (binary_expression
@ -508,7 +516,8 @@ fn field_access(x) {
arguments: (record_update_arguments
(record_update_argument
label: (label)
value: (string))
value: (string
(quoted_content)))
(record_update_argument
label: (label)
value: (integer))))))
@ -672,7 +681,8 @@ fn trial(x, y, z) {
name: (type_identifier)
arguments: (record_pattern_arguments
(record_pattern_argument
pattern: (string)))))
pattern: (string
(quoted_content))))))
(pattern_spread)))))
value: (record
name: (type_identifier)))
@ -731,7 +741,8 @@ fn try_try_again(x, y) -> Int {
name: (type_identifier))
value: (todo))
(try
pattern: (string)
pattern: (string
(quoted_content))
value: (todo))
(try
pattern: (integer)
@ -849,7 +860,8 @@ fn complex_data_fun(x, y) {
index: (integer))
arguments: (arguments
(argument
value: (string))
value: (string
(quoted_content)))
(argument
value: (integer))))
(function_call
@ -864,7 +876,8 @@ fn complex_data_fun(x, y) {
(argument
value: (float))
(argument
value: (string)))))))
value: (string
(quoted_content))))))))
================================================================================
Unusual function invocations
@ -896,7 +909,8 @@ fn weird(x) {
arguments: (arguments
(argument
label: (label)
value: (string))))
value: (string
(quoted_content)))))
(function_call
function: (anonymous_function
parameters: (function_parameters

@ -26,7 +26,8 @@ if javascript {
value: (integer))
(constant
name: (identifier)
value: (string))))
value: (string
(quoted_content)))))
========================
Target group edge cases

@ -78,7 +78,8 @@ if javascript {
(argument
value: (identifier))
(argument
value: (string))
value: (string
(quoted_content)))
(argument
value: (string)))))))))
(target_group
@ -92,8 +93,10 @@ if javascript {
return_type: (type
name: (type_identifier))
body: (external_function_body
(string)
(string))))
(string
(quoted_content))
(string
(quoted_content)))))
(target_group
target: (target)
(external_function
@ -105,8 +108,10 @@ if javascript {
return_type: (type
name: (type_identifier))
body: (external_function_body
(string)
(string)))))
(string
(quoted_content))
(string
(quoted_content))))))
================================================================================
Excerpt from stdlib's bool.gleam
@ -252,7 +257,8 @@ fn foo(a,) {
(arguments
(argument
(label)
(string)))))
(string
(quoted_content))))))
(type_definition
(type_name
(type_identifier))
@ -273,8 +279,10 @@ fn foo(a,) {
(type
(type_identifier))
(external_function_body
(string)
(string)))
(string
(quoted_content))
(string
(quoted_content))))
(function
(identifier)
(function_parameters
@ -299,7 +307,8 @@ fn foo(a,) {
(record_update_arguments
(record_update_argument
(label)
(string)))))
(string
(quoted_content))))))
(let
(tuple_pattern
(identifier))

@ -582,7 +582,10 @@ module.exports = grammar({
_type_alias: ($) => seq($.type_name, "=", $._type),
/* Literals */
string: ($) => /\"(?:\\[efnrt\"\\]|[^\"])*\"/,
string: ($) => seq('"', repeat($._string_part), '"'),
_string_part: ($) => choice($.escape_sequence, $.quoted_content),
escape_sequence: ($) => /\\[efnrt\"\\]/,
quoted_content: ($) => /(?:[^\\\"]|\\[^efnrt\"\\])+/,
float: ($) => /-?[0-9_]+\.[0-9_]+/,
integer: ($) =>
seq(optional("-"), choice($._hex, $._decimal, $._octal, $._binary)),

39
src/grammar.json vendored

@ -5144,8 +5144,45 @@
]
},
"string": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_string_part"
}
},
{
"type": "STRING",
"value": "\""
}
]
},
"_string_part": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "escape_sequence"
},
{
"type": "SYMBOL",
"name": "quoted_content"
}
]
},
"escape_sequence": {
"type": "PATTERN",
"value": "\\\\[efnrt\\\"\\\\]"
},
"quoted_content": {
"type": "PATTERN",
"value": "\\\"(?:\\\\[efnrt\\\"\\\\]|[^\\\"])*\\\""
"value": "(?:[^\\\\\\\"]|\\\\[^efnrt\\\"\\\\])+"
},
"float": {
"type": "PATTERN",

@ -3275,6 +3275,25 @@
]
}
},
{
"type": "string",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "escape_sequence",
"named": true
},
{
"type": "quoted_content",
"named": true
}
]
}
},
{
"type": "target",
"named": true,
@ -4128,6 +4147,10 @@
"type": "!=",
"named": false
},
{
"type": "\"",
"named": false
},
{
"type": "#",
"named": false
@ -4340,6 +4363,10 @@
"type": "erlang",
"named": false
},
{
"type": "escape_sequence",
"named": true
},
{
"type": "external",
"named": false
@ -4380,6 +4407,10 @@
"type": "pub",
"named": false
},
{
"type": "quoted_content",
"named": true
},
{
"type": "size",
"named": false
@ -4388,10 +4419,6 @@
"type": "statement_comment",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "todo",
"named": false

37438
src/parser.c vendored

File diff suppressed because it is too large Load Diff