Make grammar parse everything except inline M.

This commit is contained in:
Kirill Andreev 2020-08-12 22:49:05 +04:00
parent 78c408b76a
commit 28e16ca068
No known key found for this signature in database
GPG Key ID: CF7DA79DE4785A47

View File

@ -11,40 +11,45 @@ function mkOp($, opExpr) {
module.exports = grammar({ module.exports = grammar({
name: 'CameLigo', name: 'CameLigo',
extras: $ => [$.comment, $.ocaml_comment, /\s/], word: $ => $.Keyword,
extras: $ => [$.ocaml_comment, $.comment, /\s/],
rules: { rules: {
contract: $ => repeat($._declaration), contract: $ => repeat($._declaration),
_declaration: $ => choice( _declaration: $ => choice(
$.let_decl, $.let_decl,
$.type_decl $.type_decl,
$.include,
), ),
type_annot: $ => seq( include: $ => seq('#include', field("filename", $.String)),
":",
field("annotExpr", $.type_expr) _attribute: $ => /\[@@[a-z]+\]/,
),
argument: $ => seq( argument: $ => seq(
"(", "(",
field("argPattern", $._pattern), field("argPattern", $._pattern),
field("argAnnot", $.type_annot), ":",
field("annotExpr", $.type_expr),
")" ")"
), ),
let_decl: $ => seq( let_decl: $ => seq(
"let", "let",
field("binder", $._binder), field("binder", $._binder),
optional(field("bindAnnot", $.type_annot)), optional(seq(
":",
field("annotExpr", $.type_expr)
)),
"=", "=",
field("letExpr",$._let_expr) field("letExpr",$._let_expr),
repeat(field("attribute", $._attribute))
), ),
_binder: $ => choice( _binder: $ => choice(
field("letBinds", $.let_binds), field("letBinds", $.let_binds),
field("letPat", $.let_pat) field("letPat", $._pattern)
), ),
//========== EXPR ============ //========== EXPR ============
@ -60,14 +65,8 @@ module.exports = grammar({
repeat(field("bindArgument", $.argument)) repeat(field("bindArgument", $.argument))
)), )),
let_pat: $ => $._pattern,
let_expr1: $ => seq( let_expr1: $ => seq(
"let", $.let_decl,
choice(field("letBinds", $.let_binds), field("letPat", $.let_pat)),
optional(field("bindAnnot", $.type_annot)),
"=",
field("letExpr",$._expr),
"in", "in",
field("innerExpr", $._let_expr) field("innerExpr", $._let_expr)
), ),
@ -75,7 +74,7 @@ module.exports = grammar({
// [1;2] // [1;2]
list_pattern: $ => seq( list_pattern: $ => seq(
"[", "[",
sepBy1(';', field("patternListItem", $._pattern)), sepBy(';', field("patternListItem", $._pattern)),
"]" "]"
), ),
@ -114,6 +113,10 @@ module.exports = grammar({
paren_pattern: $ => seq( paren_pattern: $ => seq(
"(", "(",
field("innerPattern", $._pattern), field("innerPattern", $._pattern),
optional(seq(
":",
$.type_expr,
)),
")" ")"
), ),
@ -142,10 +145,10 @@ module.exports = grammar({
), ),
// f a // f a
fun_app: $ => prec.left(20, seq(field("appF", $.sub_expr), field("appArg",$.sub_expr))), fun_app: $ => prec.left(20, seq(field("appF", $._sub_expr), field("appArg",$._sub_expr))),
// a.0 // a.0
index_accessor: $ => prec.right(21, seq(field("exp", $.sub_expr), ".", field("ix", $.sub_expr))), index_accessor: $ => prec.right(21, seq(field("exp", $._sub_expr), ".", field("ix", $._sub_expr))),
// { p with a = b; c = d } // { p with a = b; c = d }
rec_expr: $ => seq( rec_expr: $ => seq(
@ -164,14 +167,16 @@ module.exports = grammar({
), ),
// if a then b else c // if a then b else c
if_expr: $ => seq( if_expr: $ => prec.right(seq(
"if", "if",
field("condition", $._expr), field("condition", $._expr),
"then", "then",
field("thenBranch", $._expr), field("thenBranch", $._let_expr),
optional(seq(
"else", "else",
field("elseBranch", $._expr) field("elseBranch", $._let_expr)
), ))
)),
// match x with ... // match x with ...
match_expr: $ => prec.right(1,seq( match_expr: $ => prec.right(1,seq(
@ -186,7 +191,7 @@ module.exports = grammar({
matching: $ => seq( matching: $ => seq(
field("pattern", $._pattern), field("pattern", $._pattern),
"->", "->",
field("matchingExpr", $._expr) field("matchingExpr", $._let_expr)
), ),
lambda_expr: $ => seq( lambda_expr: $ => seq(
@ -210,11 +215,11 @@ module.exports = grammar({
_expr: $ => choice( _expr: $ => choice(
$.call, $.call,
$.sub_expr, $._sub_expr,
$.tup_expr $.tup_expr
), ),
sub_expr: $ => choice( _sub_expr: $ => choice(
$.fun_app, $.fun_app,
$.paren_expr, $.paren_expr,
$.Name, $.Name,
@ -225,13 +230,23 @@ module.exports = grammar({
$.lambda_expr, $.lambda_expr,
$.match_expr, $.match_expr,
$.list_expr, $.list_expr,
$.index_accessor $.index_accessor,
$.block_expr,
),
block_expr: $ => seq(
"begin",
sepBy(";", field("elem", $._let_expr)),
"end",
), ),
paren_expr: $ => seq( paren_expr: $ => seq(
"(", "(",
field("innerExpr", $._expr), field("innerExpr", $._let_expr),
optional(field("exprAnnot", $.type_annot)), optional(seq(
":",
field("annotExpr", $.type_expr)
)),
")" ")"
), ),
@ -247,7 +262,7 @@ module.exports = grammar({
field("argument", $.type_expr), field("argument", $.type_expr),
seq( seq(
"(", "(",
sepBy1(",", field("argument", $.type_expr)), sepBy1(",", field("argument", choice($.type_expr, $.String))),
")" ")"
) )
), ),
@ -292,6 +307,7 @@ module.exports = grammar({
// Cat of string | Personn of string * string // Cat of string | Personn of string * string
type_sum: $ => seq( type_sum: $ => seq(
optional('|'),
sepBy1('|', field("variant", $.variant)), sepBy1('|', field("variant", $.variant)),
), ),
@ -355,19 +371,18 @@ module.exports = grammar({
True: $ => 'true', True: $ => 'true',
Unit: $ => '()', Unit: $ => '()',
comment: $ => /\/\/[^\n]*\n/, comment: $ => /\/\/(\*\)[^\n]|\*[^\)\n]|[^\*\n])*\n/,
ocaml_comment: $ => ocaml_comment: $ =>
seq( seq(
'(*', '(*',
repeat(choice( repeat(choice(
$.ocaml_comment, $.ocaml_comment,
/'([^'\\]|\\[\\"'ntbr ]|\\[0-9][0-9][0-9]|\\x[0-9A-Fa-f][0-9A-Fa-f]|\\o[0-3][0-7][0-7])'/, /[^\*\(]/,
/"([^\\"]|\\(.|\n))*"/, /\*[^\)]/,
/[A-Za-z_][a-zA-Z0-9_']*/, /\([^\*]/,
/[^('"{*A-Za-z_]+/,
'(', "'", '*',
)), )),
'*)' /\*+\)/
), ),
} }
}); });