Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Add support for static and class functions + variables
Browse files Browse the repository at this point in the history
  • Loading branch information
justin committed Oct 25, 2019
1 parent be2898a commit a22fa5e
Show file tree
Hide file tree
Showing 5 changed files with 15,241 additions and 13,638 deletions.
27 changes: 27 additions & 0 deletions corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private let foo
public let foo = bar
let foo, bar = foo
let foo, bar = foo, quux: Int = baz
public static let foo = bar

---

Expand All @@ -42,6 +43,9 @@ let foo, bar = foo, quux: Int = baz
value: (identifier)
name: (identifier)
type: (type (standard_type))
value: (identifier))
(constant_declaration (modifier) (modifier)
name: (identifier)
value: (identifier)))

======
Expand All @@ -55,6 +59,8 @@ var foo: Int { get set }
var foo: Int { set get }
var foo, bar = foo
public var foo, bar = foo, quux: Int = baz
public private(set) var foo = bar
public static var foo = bar

---

Expand Down Expand Up @@ -83,6 +89,12 @@ public var foo, bar = foo, quux: Int = baz
value: (identifier)
name: (identifier)
type: (type (standard_type))
value: (identifier))
(variable_declaration (modifier) (modifier)
name: (identifier)
value: (identifier))
(variable_declaration (modifier) (modifier)
name: (identifier)
value: (identifier)))

======
Expand Down Expand Up @@ -226,6 +238,21 @@ public func foo<Foo, Bar>(p1: Foo) -> Bar {}
(parameter_declaration (identifier) (type (identifier))))
return: (type (identifier))))


======
functions - static
======

public static func foo() {}

---

(program
(function_declaration (modifier) (modifier)
name: (identifier)
(parameter_list))
)

======
enum
======
Expand Down
19 changes: 15 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ module.exports = grammar({
)
),

constant_declaration: $ => seq(optional($.modifier), 'let', commaSep1($._pattern_initializer)),
constant_declaration: $ => seq(repeat($.modifier), 'let', commaSep1($._pattern_initializer)),

_pattern_initializer: $ => seq(
field('name', $._pattern),
Expand All @@ -214,7 +214,7 @@ module.exports = grammar({
)
),

_variable_declaration_head: $ => seq(optional($.modifier), 'var'),
_variable_declaration_head: $ => seq(repeat($.modifier), 'var'),

_variable_name: $ => $.identifier,

Expand All @@ -240,7 +240,7 @@ module.exports = grammar({
),

_function_head: $ => seq(
optional($.modifier),
repeat($.modifier),
'func',
field('name', choice($.identifier, $.operator))
),
Expand Down Expand Up @@ -304,12 +304,23 @@ module.exports = grammar({
),

modifier: $ => choice(
$._access_control_modifier,
$._declaration_modifier,
),

_declaration_modifier: $ => choice(
'static',
'final',
'class'
),

_access_control_modifier: $ => choice(
'public',
'private',
'fileprivate',
'open',
'internal',
'final'
...['private', 'fileprivate', 'internal'].map(n => `${n}(set)`),
),

generic_clause: $ => seq(
Expand Down
85 changes: 54 additions & 31 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1556,16 +1556,11 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "modifier"
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "modifier"
}
},
{
"type": "STRING",
Expand Down Expand Up @@ -1729,16 +1724,11 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "modifier"
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "modifier"
}
},
{
"type": "STRING",
Expand Down Expand Up @@ -1891,16 +1881,11 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "modifier"
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "modifier"
}
},
{
"type": "STRING",
Expand Down Expand Up @@ -2351,6 +2336,36 @@
]
},
"modifier": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_access_control_modifier"
},
{
"type": "SYMBOL",
"name": "_declaration_modifier"
}
]
},
"_declaration_modifier": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "static"
},
{
"type": "STRING",
"value": "final"
},
{
"type": "STRING",
"value": "class"
}
]
},
"_access_control_modifier": {
"type": "CHOICE",
"members": [
{
Expand All @@ -2375,7 +2390,15 @@
},
{
"type": "STRING",
"value": "final"
"value": "private(set)"
},
{
"type": "STRING",
"value": "fileprivate(set)"
},
{
"type": "STRING",
"value": "internal(set)"
}
]
},
Expand Down
18 changes: 17 additions & 1 deletion src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
}
},
"children": {
"multiple": false,
"multiple": true,
"required": false,
"types": [
{
Expand Down Expand Up @@ -4646,6 +4646,10 @@
"type": "fileprivate",
"named": false
},
{
"type": "fileprivate(set)",
"named": false
},
{
"type": "final",
"named": false
Expand Down Expand Up @@ -4710,6 +4714,10 @@
"type": "internal",
"named": false
},
{
"type": "internal(set)",
"named": false
},
{
"type": "is",
"named": false
Expand Down Expand Up @@ -4778,6 +4786,10 @@
"type": "private",
"named": false
},
{
"type": "private(set)",
"named": false
},
{
"type": "protocol",
"named": false
Expand Down Expand Up @@ -4818,6 +4830,10 @@
"type": "standard_type",
"named": true
},
{
"type": "static",
"named": false
},
{
"type": "static_string_literal",
"named": true
Expand Down
Loading

0 comments on commit a22fa5e

Please sign in to comment.