Skip to content

Add in support for boolean, character, and symbol literals. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build/test
on:
push:
pull_request:
branches:
- master
pull_request:
jobs:
test:
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 2 additions & 2 deletions corpus/definitions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,6 @@ def mkLines(header: String, indented: Boolean = false, embraced: Boolean = false
(identifier)
(parameters
(parameter (identifier) (type_identifier))
(parameter (identifier) (type_identifier) (identifier))
(parameter (identifier) (type_identifier) (identifier)))
(parameter (identifier) (type_identifier) (boolean_literal))
(parameter (identifier) (type_identifier) (boolean_literal)))
(type_identifier) (block)))
66 changes: 66 additions & 0 deletions corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,69 @@ val string = 3.14

(compilation_unit
(val_definition (identifier) (number)))

==========================
Boolean literals
==========================

val myBool = true

def foo(a: Boolean = false) = a && true

---

(compilation_unit
(val_definition (identifier) (boolean_literal))
(function_definition
(identifier)
(parameters (parameter
(identifier)
(type_identifier)
(boolean_literal)))
(infix_expression
(identifier)
(operator_identifier)
(boolean_literal))))

==========================
Character literals
==========================

val myChar = 'c'

val otherChar = '\u0041'

val anotherChar = '\n'

def foo(a: Char = 'c') = a + 'd'

---

(compilation_unit
(val_definition (identifier) (character_literal))
(val_definition (identifier) (character_literal))
(val_definition (identifier) (character_literal))
(function_definition
(identifier)
(parameters (parameter
(identifier)
(type_identifier)
(character_literal)))
(infix_expression
(identifier)
(operator_identifier)
(character_literal))))

==========================
Symobl literals
==========================

val mySymbol = 'c

val myOtherSymbol = 'thing

---

(compilation_unit
(val_definition (identifier) (symbol_literal))
(val_definition (identifier) (symbol_literal)))
27 changes: 27 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ module.exports = grammar({
$.alternative_pattern,
$.typed_pattern,
$.number,
$.boolean_literal,
$.character_literal,
$.symbol_literal,
$.string,
$.wildcard
),
Expand Down Expand Up @@ -519,6 +522,9 @@ module.exports = grammar({
$.block,
$.identifier,
$.number,
$.boolean_literal,
$.character_literal,
$.symbol_literal,
$.string
),

Expand Down Expand Up @@ -640,6 +646,27 @@ module.exports = grammar({

number: $ => /[\d\.]+/,

boolean_literal: $ => choice('true', 'false'),

character_literal: $ => token(seq(
'\'',
optional(choice(
seq('\\', choice(
/[^xu]/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/,
/x[0-9a-fA-F]{2}/
)),
/[^\\'\n]/
)),
'\''
)),
Comment on lines +651 to +663
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on this a bunch trying to exactly get what is described here, and finally I just went with a modified version of what tree-sitter-rust was using.


symbol_literal: $ => token(seq(
"'",
repeat1(/[^\\'\n]/)
)),

interpolated_string_expression: $ => seq(
$.identifier,
$.interpolated_string
Expand Down
119 changes: 119 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,18 @@
"type": "SYMBOL",
"name": "number"
},
{
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "SYMBOL",
"name": "character_literal"
},
{
"type": "SYMBOL",
"name": "symbol_literal"
},
{
"type": "SYMBOL",
"name": "string"
Expand Down Expand Up @@ -2686,6 +2698,18 @@
"type": "SYMBOL",
"name": "number"
},
{
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "SYMBOL",
"name": "character_literal"
},
{
"type": "SYMBOL",
"name": "symbol_literal"
},
{
"type": "SYMBOL",
"name": "string"
Expand Down Expand Up @@ -3338,6 +3362,101 @@
"type": "PATTERN",
"value": "[\\d\\.]+"
},
"boolean_literal": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "true"
},
{
"type": "STRING",
"value": "false"
}
]
},
"character_literal": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^xu]"
},
{
"type": "PATTERN",
"value": "u[0-9a-fA-F]{4}"
},
{
"type": "PATTERN",
"value": "u{[0-9a-fA-F]+}"
},
{
"type": "PATTERN",
"value": "x[0-9a-fA-F]{2}"
}
]
}
]
},
{
"type": "PATTERN",
"value": "[^\\\\'\\n]"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "'"
}
]
}
},
"symbol_literal": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "REPEAT1",
"content": {
"type": "PATTERN",
"value": "[^\\\\'\\n]"
}
}
]
}
},
"interpolated_string_expression": {
"type": "SEQ",
"members": [
Expand Down
45 changes: 45 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"type": "block",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "call_expression",
"named": true
Expand All @@ -77,6 +81,10 @@
"type": "case_block",
"named": true
},
{
"type": "character_literal",
"named": true
},
{
"type": "field_expression",
"named": true
Expand Down Expand Up @@ -125,6 +133,10 @@
"type": "string",
"named": true
},
{
"type": "symbol_literal",
"named": true
},
{
"type": "try_expression",
"named": true
Expand All @@ -143,6 +155,10 @@
"type": "alternative_pattern",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "capture_pattern",
"named": true
Expand All @@ -151,6 +167,10 @@
"type": "case_class_pattern",
"named": true
},
{
"type": "character_literal",
"named": true
},
{
"type": "identifier",
"named": true
Expand All @@ -167,6 +187,10 @@
"type": "string",
"named": true
},
{
"type": "symbol_literal",
"named": true
},
{
"type": "tuple_pattern",
"named": true
Expand Down Expand Up @@ -298,6 +322,11 @@
]
}
},
{
"type": "boolean_literal",
"named": true,
"fields": {}
},
{
"type": "call_expression",
"named": true,
Expand Down Expand Up @@ -3154,6 +3183,10 @@
"type": "catch",
"named": false
},
{
"type": "character_literal",
"named": true
},
{
"type": "class",
"named": false
Expand All @@ -3174,6 +3207,10 @@
"type": "extends",
"named": false
},
{
"type": "false",
"named": false
},
{
"type": "final",
"named": false
Expand Down Expand Up @@ -3242,10 +3279,18 @@
"type": "sealed",
"named": false
},
{
"type": "symbol_literal",
"named": true
},
{
"type": "trait",
"named": false
},
{
"type": "true",
"named": false
},
{
"type": "try",
"named": false
Expand Down
Loading