Skip to content

Commit

Permalink
Parse newer commits of example repos, fix several parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunsfeld committed Jul 24, 2020
1 parent 61e7322 commit 320ddc2
Show file tree
Hide file tree
Showing 7 changed files with 241,982 additions and 272,008 deletions.
16 changes: 16 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ Command strings
(command_string)
(command_string))

=================
Prefixed strings
=================

x = r"foo"
y = v"1.0"

---

(source_file
(assignment_expression (identifier) (string (identifier)))
(assignment_expression (identifier) (string (identifier))))

=================
Characters
=================
Expand Down Expand Up @@ -164,6 +177,8 @@ Macro calls
@assert x == y
@assert x == y "a message"

@. a

@testset "a" begin
b = c
end
Expand All @@ -180,6 +195,7 @@ end
(macro_argument_list
(binary_expression (identifier) (identifier))
(string)))
(macro_expression (macro_identifier (operator)) (macro_argument_list (identifier)))
(macro_expression
(macro_identifier (identifier))
(macro_argument_list
Expand Down
162 changes: 86 additions & 76 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ grammar({

inline: $ => [
$._terminator,
$._expression_list,
$._definition,
$._statement,
],

supertypes: $ => [
$._statement,
$._definition,
$._expression,
$._primary_expression,
],

externals: $ => [
$.block_comment,
$.triple_string,
Expand All @@ -88,11 +94,11 @@ grammar({

conflicts: $ => [
// Arrow functions vs tuples
[$._expression, $.parameter_list],
[$._expression, $.spread_parameter],
[$._expression, $.typed_parameter],
[$._expression, $.named_field],
[$._expression, $.named_field, $.optional_parameter],
[$._primary_expression, $.parameter_list],
[$._primary_expression, $.spread_parameter],
[$._primary_expression, $.typed_parameter],
[$._primary_expression, $.named_field],
[$._primary_expression, $.named_field, $.optional_parameter],
[$.named_field, $.optional_parameter],
],

Expand Down Expand Up @@ -133,27 +139,27 @@ grammar({

function_definition: $ => seq(
'function',
$.identifier,
optional($.type_parameter_list),
$.parameter_list,
field('name', $.identifier),
field('type_parameters', optional($.type_parameter_list)),
field('parametere', $.parameter_list),
optional($._expression_list),
'end'
),

abstract_definition: $ => seq(
'abstract',
'type',
$.identifier,
optional($.type_parameter_list),
field('name', $.identifier),
field('type_parameters', optional($.type_parameter_list)),
optional($.subtype_clause),
'end'
),

primitive_definition: $ => seq(
'primitive',
'type',
$.identifier,
optional($.type_parameter_list),
field('name', $.identifier),
field('type_parameters', optional($.type_parameter_list)),
optional($.subtype_clause),
alias(/[1-9][0-9]*/, $.number),
'end'
Expand All @@ -162,24 +168,24 @@ grammar({
struct_definition: $ => seq(
optional('mutable'),
'struct',
$.identifier,
optional($.type_parameter_list),
field('name', $.identifier),
field('type_parameters', optional($.type_parameter_list)),
optional($.subtype_clause),
optional($._expression_list),
'end'
),

module_definition: $ => seq(
'module',
$.identifier,
field('name', $.identifier),
optional($._expression_list),
'end'
),

macro_definition: $ => seq(
'macro',
$.identifier,
$.parameter_list,
field('name', choice($.identifier, $.operator)),
field('parameters', $.parameter_list),
optional($._expression_list),
'end'
),
Expand Down Expand Up @@ -227,9 +233,9 @@ grammar({
),

constrained_parameter: $ => seq(
$.identifier,
field('name', $.identifier),
'<:',
$._expression
field('value', $._expression)
),

subtype_clause: $ => seq(
Expand All @@ -256,17 +262,17 @@ grammar({

if_statement: $ => seq(
'if',
$._expression,
field('condition', $._expression),
optional($._terminator),
optional($._expression_list),
repeat($.elseif_clause),
optional($.else_clause),
field('alternative', repeat($.elseif_clause)),
field('alternative', optional($.else_clause)),
'end'
),

elseif_clause: $ => seq(
'elseif',
$._expression,
field('condition', $._expression),
optional($._terminator),
optional($._expression_list)
),
Expand Down Expand Up @@ -299,15 +305,15 @@ grammar({

for_statement: $ => seq(
'for',
$.for_binding,
sep1(',', $.for_binding),
optional($._terminator),
optional($._expression_list),
'end'
),

while_statement: $ => seq(
'while',
$._expression,
field('condition', $._expression),
optional($._terminator),
optional($._expression_list),
'end'
Expand Down Expand Up @@ -383,75 +389,66 @@ grammar({
_expression: $ => choice(
$._statement,
$._definition,
$.parenthesized_expression,
$.typed_expression,
$.compound_expression,
$.pair_expression,
$.field_expression,
$.subscript_expression,
alias(':', $.operator),
$.macro_expression,
$.call_expression,
$.broadcast_call_expression,
$.unary_expression,
$.binary_expression,
$.ternary_expression,
$.parameterized_identifier,
$.array_expression,
$.matrix_expression,
$.tuple_expression,
$.generator_expression,
$.array_comprehension_expression,
$.function_expression,
$.coefficient_expression,
$.spread_expression,
$.range_expression,
$.quote_expression,
$.interpolation_expression,
$.number,
$._primary_expression,
),

_primary_expression: $ => choice(
$.identifier,
$.operator,
$.number,
$.string,
$.command_string,
$.character,
$.triple_string,
$.array_expression,
$.array_comprehension_expression,
$.matrix_expression,
$.call_expression,
$.field_expression,
$.parenthesized_expression,
$.subscript_expression,
$.parameterized_identifier,
$.tuple_expression,
$.broadcast_call_expression,
),

bare_tuple_expression: $ => prec(-1, seq(
$._expression,
repeat1(prec(-1, seq(',', $._expression)))
)),

operator: $ => choice(':', '+', $._plus_operator, $._times_operator, $._power_operator),
operator: $ => choice('+', $._plus_operator, $._times_operator, $._power_operator),

parenthesized_expression: $ => prec(1, seq(
'(', choice($._expression_list, $.spread_expression), ')'
)),

field_expression: $ => prec(PREC.dot, seq(
choice(
$.identifier,
$.array_expression,
$.tuple_expression,
$.field_expression,
$.call_expression,
$.subscript_expression,
$.parenthesized_expression
),
$._primary_expression,
'.',
$.identifier
)),

subscript_expression: $ => seq(
choice(
$.identifier,
$.array_expression,
$.tuple_expression,
$.field_expression,
$.call_expression,
$.parenthesized_expression
),
$._primary_expression,
token.immediate('['),
$._expression,
sep(',', $._expression),
optional(','),
']'
),

Expand Down Expand Up @@ -479,25 +476,14 @@ grammar({
),

call_expression: $ => prec(PREC.call, seq(
choice(
$.identifier,
$.parameterized_identifier,
$.field_expression,
$.call_expression,
$.parenthesized_expression
),
$._primary_expression,
$._immediate_paren,
choice($.argument_list, $.generator_expression),
optional($.do_clause)
)),

broadcast_call_expression: $ => prec(PREC.call, seq(
choice(
$.identifier,
$.parameterized_identifier,
$.field_expression,
$.call_expression
),
$._primary_expression,
'.',
$._immediate_paren,
choice($.argument_list, $.generator_expression),
Expand All @@ -524,6 +510,7 @@ grammar({
';',
sep1(',', alias($.named_field, $.named_argument))
)),
optional(','),
')'
),

Expand Down Expand Up @@ -586,6 +573,16 @@ grammar({
$._arrow_operator,
$._expression
)),
prec.right(PREC.pipe_left, seq(
$._expression,
'<|',
$._expression
)),
prec.left(PREC.pipe_right, seq(
$._expression,
'|>',
$._expression
)),
prec.left(PREC.comparison, seq(
$._expression,
choice('in', 'isa', $._comparison_operator),
Expand Down Expand Up @@ -726,7 +723,11 @@ grammar({

// Tokens

macro_identifier: $ => seq('@', $.identifier),
macro_identifier: $ => seq('@', choice(
$.identifier,
$.operator,
alias('.', $.operator)
)),

identifier: $ => {
const operators = [
Expand Down Expand Up @@ -756,7 +757,7 @@ grammar({

// First char: ASCII letter, Greek letter, Extended Latin letter, or ∇
// Remaining characters: not delimiter, not operator
return new RegExp(`[_a-zA-ZͰ-ϿĀ-ſ∇][^\\s\\.\\-\\[\\]${operatorCharacters}]*`)
return new RegExp(`[_a-zA-ZͰ-ϿĀ-ſ∇][^"'\\s\\.\\-\\[\\]${operatorCharacters}]*`)
},

number: $ => {
Expand All @@ -772,11 +773,20 @@ grammar({
))
},

string: $ => token(seq(
'"',
repeat(choice(/[^"\\\n]/, /\\./)),
'"'
)),
string: $ => seq(
choice(
'"',
seq(
field('prefix', $.identifier),
token.immediate('"')
)
),
optional(token.immediate(repeat1(choice(
/[^"\\\n]/,
/\\./
)))),
token.immediate('"'),
),

command_string: $ => token(seq(
'`',
Expand Down
Loading

0 comments on commit 320ddc2

Please sign in to comment.