Skip to content

Commit

Permalink
Adds necessary logic to support attributes. Follows the PHP interpren…
Browse files Browse the repository at this point in the history
…ters lead on disallowing comments starting with #[.
  • Loading branch information
cfroystad committed May 4, 2021
1 parent a1426c5 commit a45f409
Show file tree
Hide file tree
Showing 6 changed files with 78,504 additions and 73,425 deletions.
31 changes: 28 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ module.exports = grammar({
),

class_declaration: $ => prec.right(seq(
optional(field('attributes', $.attribute_list)),
optional($.class_modifier),
keyword('class'),
field('name', $.name),
Expand All @@ -270,20 +271,28 @@ module.exports = grammar({
),

_member_declaration: $ => choice(
$.const_declaration,
alias($._class_const_declaration, $.const_declaration),
$.property_declaration,
$.method_declaration,
$.use_declaration
),

const_declaration: $ => seq(
const_declaration: $ => $._const_declaration,

_class_const_declaration: $ => seq(
optional(field('attributes', $.attribute_list)),
$._const_declaration
),

_const_declaration: $ => seq(
optional($.visibility_modifier),
keyword('const'),
commaSep1($.const_element),
$._semicolon
),

property_declaration: $ => seq(
optional(field('attributes', $.attribute_list)),
repeat1($._modifier),
optional(field('type', $._type)),
commaSep1($.property_element),
Expand All @@ -306,6 +315,7 @@ module.exports = grammar({
),

method_declaration: $ => seq(
optional(field('attributes', $.attribute_list)),
repeat($._modifier),
$._function_definition_header,
choice(
Expand Down Expand Up @@ -363,6 +373,7 @@ module.exports = grammar({
),

function_definition: $ => seq(
optional(field('attributes', $.attribute_list)),
$._function_definition_header,
field('body', $.compound_statement)
),
Expand Down Expand Up @@ -393,6 +404,7 @@ module.exports = grammar({
),

simple_parameter: $ => seq(
optional(field('attributes', $.attribute_list)),
field('type', optional($._type)),
optional('&'),
field('name', $.variable_name),
Expand All @@ -403,6 +415,7 @@ module.exports = grammar({
),

variadic_parameter: $ => seq(
optional(field('attributes', $.attribute_list)),
field('type', optional($._type)),
optional('&'),
'...',
Expand Down Expand Up @@ -1073,6 +1086,17 @@ module.exports = grammar({
seq('[', commaSep($.array_element_initializer), optional(','), ']')
),

attribute_list: $ => repeat1(seq(
'#[',
commaSep1($.attribute),
']',
)),

attribute: $ => seq(
$.qualified_name,
optional(field('parameters', $.arguments))
),

string: $ => {
const b_prefix = /[bB]/
const single_quote_chars = repeat(/\\'|\\\\|\\?[^'\\]/)
Expand Down Expand Up @@ -1191,10 +1215,11 @@ module.exports = grammar({

comment: $ => token(choice(
seq(
choice('//', '#'),
choice('//', /#[^?\[?\r?\n]/),
repeat(/[^?\r?\n]|\?[^>\r\n]/),
optional(/\?\r?\n/)
),
'#',
seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
Expand Down
208 changes: 204 additions & 4 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,22 @@
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -1081,8 +1097,13 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "const_declaration"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_class_const_declaration"
},
"named": true,
"value": "const_declaration"
},
{
"type": "SYMBOL",
Expand All @@ -1099,6 +1120,35 @@
]
},
"const_declaration": {
"type": "SYMBOL",
"name": "_const_declaration"
},
"_class_const_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_const_declaration"
}
]
},
"_const_declaration": {
"type": "SEQ",
"members": [
{
Expand Down Expand Up @@ -1156,6 +1206,22 @@
"property_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT1",
"content": {
Expand Down Expand Up @@ -1268,6 +1334,22 @@
"method_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
Expand Down Expand Up @@ -1545,6 +1627,22 @@
"function_definition": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_function_definition_header"
Expand Down Expand Up @@ -1776,6 +1874,22 @@
"simple_parameter": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "type",
Expand Down Expand Up @@ -1842,6 +1956,22 @@
"variadic_parameter": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "attributes",
"content": {
"type": "SYMBOL",
"name": "attribute_list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "type",
Expand Down Expand Up @@ -5709,6 +5839,72 @@
}
]
},
"attribute_list": {
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "attribute"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "attribute"
}
]
}
}
]
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"attribute": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "qualified_name"
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "SYMBOL",
"name": "arguments"
}
},
{
"type": "BLANK"
}
]
}
]
},
"string": {
"type": "TOKEN",
"content": {
Expand Down Expand Up @@ -7120,8 +7316,8 @@
"value": "//"
},
{
"type": "STRING",
"value": "#"
"type": "PATTERN",
"value": "#[^?\\[?\\r?\\n]"
}
]
},
Expand All @@ -7146,6 +7342,10 @@
}
]
},
{
"type": "STRING",
"value": "#"
},
{
"type": "SEQ",
"members": [
Expand Down
Loading

0 comments on commit a45f409

Please sign in to comment.