new syntax, also block comments are totally broken

This commit is contained in:
William Ball 2024-12-01 20:53:58 -08:00
parent 6e1878db5d
commit 6de6e3d801
13 changed files with 1786 additions and 1761 deletions

View file

@ -12,30 +12,17 @@ module.exports = grammar({
extras: $ => [ extras: $ => [
$.comment, $.comment,
$.block_comment,
/\s/, /\s/,
], ],
rules: { rules: {
program : $ => repeat(choice($.definition, $.preprocess)), program : $ => repeat(choice($.definition, $.preprocess, $.axiom)),
identifier : $ => /[a-zA-Z_]\w*/, identifier : $ => /[a-zA-Z_]\w*/,
comment : $ => token(seq('--', /.*/)), comment : $ => token(seq('--', /.*/)),
block_comment: $ => token(seq(
'[*',
repeat(choice(
/[^\[\]*]/,
/\[[^*]/,
/\*[^\]]/,
/\[\*/,
/\*\]/,
)),
'*]'
)),
param_block : $ => seq( param_block : $ => seq(
'(', '(',
field('param', repeat1($.identifier)), field('param', repeat1($.identifier)),
@ -72,6 +59,7 @@ module.exports = grammar({
'(', '(',
$.identifier, $.identifier,
repeat($.param_block), repeat($.param_block),
optional($.ascription),
':=', ':=',
$.expr, $.expr,
')', ')',
@ -112,12 +100,21 @@ module.exports = grammar({
field('type', $.expr), field('type', $.expr),
), ),
axiom : $ => seq(
'axiom',
field('name', $.identifier),
repeat($.param_block),
$.ascription,
';'
),
definition : $ => seq( definition : $ => seq(
'def',
field('name', $.identifier), field('name', $.identifier),
repeat($.param_block), repeat($.param_block),
optional($.ascription), optional($.ascription),
':=', ':=',
choice($.expr, $.axiom), choice($.expr),
';', ';',
), ),

100
src/grammar.json generated
View file

@ -13,6 +13,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "preprocess" "name": "preprocess"
},
{
"type": "SYMBOL",
"name": "axiom"
} }
] ]
} }
@ -37,50 +41,6 @@
] ]
} }
}, },
"block_comment": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "[*"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^\\[\\]*]"
},
{
"type": "PATTERN",
"value": "\\[[^*]"
},
{
"type": "PATTERN",
"value": "\\*[^\\]]"
},
{
"type": "PATTERN",
"value": "\\[\\*"
},
{
"type": "PATTERN",
"value": "\\*\\]"
}
]
}
},
{
"type": "STRING",
"value": "*]"
}
]
}
},
"param_block": { "param_block": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -261,6 +221,18 @@
"name": "param_block" "name": "param_block"
} }
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "ascription"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "STRING", "type": "STRING",
"value": ":=" "value": ":="
@ -311,9 +283,37 @@
} }
}, },
"axiom": { "axiom": {
"type": "SEQ",
"members": [
{
"type": "STRING", "type": "STRING",
"value": "axiom" "value": "axiom"
}, },
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "param_block"
}
},
{
"type": "SYMBOL",
"name": "ascription"
},
{
"type": "STRING",
"value": ";"
}
]
},
"arrow": { "arrow": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 1, "value": 1,
@ -398,6 +398,10 @@
"definition": { "definition": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "def"
},
{ {
"type": "FIELD", "type": "FIELD",
"name": "name", "name": "name",
@ -435,10 +439,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expr" "name": "expr"
},
{
"type": "SYMBOL",
"name": "axiom"
} }
] ]
}, },
@ -475,10 +475,6 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "comment" "name": "comment"
}, },
{
"type": "SYMBOL",
"name": "block_comment"
},
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "\\s" "value": "\\s"

52
src/node-types.json generated
View file

@ -76,6 +76,36 @@
} }
} }
}, },
{
"type": "axiom",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "ascription",
"named": true
},
{
"type": "param_block",
"named": true
}
]
}
},
{ {
"type": "binding", "type": "binding",
"named": true, "named": true,
@ -84,6 +114,10 @@
"multiple": true, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "ascription",
"named": true
},
{ {
"type": "expr", "type": "expr",
"named": true "named": true
@ -122,10 +156,6 @@
"type": "ascription", "type": "ascription",
"named": true "named": true
}, },
{
"type": "axiom",
"named": true
},
{ {
"type": "expr", "type": "expr",
"named": true "named": true
@ -266,6 +296,10 @@
"multiple": true, "multiple": true,
"required": false, "required": false,
"types": [ "types": [
{
"type": "axiom",
"named": true
},
{ {
"type": "definition", "type": "definition",
"named": true "named": true
@ -347,11 +381,7 @@
}, },
{ {
"type": "axiom", "type": "axiom",
"named": true "named": false
},
{
"type": "block_comment",
"named": true
}, },
{ {
"type": "command", "type": "command",
@ -361,6 +391,10 @@
"type": "comment", "type": "comment",
"named": true "named": true
}, },
{
"type": "def",
"named": false
},
{ {
"type": "end", "type": "end",
"named": false "named": false

3305
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -2,10 +2,11 @@
Application Application
=========== ===========
foo (A B : *) (f : A -> B) (x : A) := def foo (A B : *) (f : A -> B) (x : A) :=
(fun (x : B) => x) (f x); (fun (x : B) => x) (f x);
--- ---
(program (program
(definition (definition
(identifier) (identifier)

View file

@ -2,7 +2,7 @@
Arrows Arrows
====== ======
foo (A B : *) (f : A -> A -> B) (x : A) := f x x; def foo (A B : *) (f : A -> A -> B) (x : A) := f x x;
--- ---

View file

@ -2,17 +2,16 @@
Axioms Axioms
====== ======
nat : * := axiom; axiom nat : *;
----- -----
(program (program
(definition (axiom
(identifier) (identifier)
(ascription (ascription
(expr (expr
(app_term (app_term
(app (app
(term (term
(star)))))) (star))))))))
(axiom)))

View file

@ -2,23 +2,9 @@
Comments Comments
======== ========
-- here's a comment [* foo [* nested *] *]
axiom nat : *;
foo := [* bar *]
-- and a comment in the middle
[* and a [* nested *] block comment *]
*;
--- ---
(program
(comment)
(definition
(identifier)
(comment)
(block_comment)
(expr
(app_term
(app
(term
(star)))))))

View file

@ -4,7 +4,7 @@ Include
@include foo.pg @include foo.pg
baz : * := A; def baz : * := A;
@include bar.pg @include bar.pg

View file

@ -2,7 +2,7 @@
Lambda Abstraction Lambda Abstraction
================== ==================
foo := fun (A : *) (x : A) => x; def foo := fun (A : *) (x : A) => x;
---------- ----------

View file

@ -2,7 +2,8 @@
Let Let
=== ===
foo := let (x := a) def foo :=
let (x : bar := a)
(y := x) (y := x)
(f (x : A) := x) (f (x : A) := x)
in in
@ -19,6 +20,12 @@ foo := let (x := a)
(let (let
(binding (binding
(identifier) (identifier)
(ascription
(expr
(app_term
(app
(term
(identifier))))))
(expr (expr
(app_term (app_term
(app (app

View file

@ -2,7 +2,7 @@
Pi Abstraction Pi Abstraction
============== ==============
rel := forall (A : *) (x : A), *; def rel := forall (A : *) (x : A), *;
---------- ----------

View file

@ -2,7 +2,7 @@
Definition Definition
========== ==========
foo (A : *) (x y z : A) := x; def foo (A : *) (x y z : A) := x;
--- ---