infix operators

This commit is contained in:
William Ball 2024-12-10 21:40:39 -08:00
parent 3834eb6f0a
commit 4131069cd4
16 changed files with 5502 additions and 3380 deletions

View file

@ -17,9 +17,17 @@ module.exports = grammar({
rules: {
program : $ => repeat1(choice($.definition, $.preprocess, $.axiom, $.section, $.variable)),
program : $ => repeat1(choice(
$.definition,
$.preprocess,
$.axiom,
$.section,
$.variable,
$.fixity,
)),
identifier : $ => /[a-zA-Z_]\w*/,
symbol : $ => /[!@#$%^&*-+=<>,./?\[\]{}\\|`~'\"∧∨⊙×≅]+/,
comment : $ => token(seq('--', /.*/)),
@ -29,9 +37,24 @@ module.exports = grammar({
'end', $.identifier,
),
precedence : $ => /[0-9]+/,
fixity : $ => seq(
choice('infixl', 'infixr'),
$.precedence,
$.symbol,
';'
),
variable : $ => seq(
choice('variable', 'hypothesis'),
repeat1($.param_block),
repeat1(seq(
'(',
repeat1(choice($.identifier, $.symbol)),
':',
field('type', $.expr),
')'
)),
';'
),
@ -43,7 +66,7 @@ module.exports = grammar({
')'
),
star : $ => "*",
star : $ => "",
square : $ => choice('□', '[]'),
sort : $ => choice($.star, $.square, seq($.square, /[0-9₀₁₂₃₄₅₆₇₈₉]+/)),
@ -69,6 +92,14 @@ module.exports = grammar({
seq('(', $.expr, ')'),
),
// HACK:
// completely ignore precedence and associativity for treesitter
// this is enough to get the syntax highlighting right
binex : $ => seq(
$.app,
optional(seq($.symbol, $.binex)),
),
binding : $ => seq(
'(',
$.identifier,
@ -91,7 +122,7 @@ module.exports = grammar({
arrow : $ => prec.left(1, seq(
$.app_term,
choice('->', '→'),
'→',
$.expr,
)),
@ -99,7 +130,7 @@ module.exports = grammar({
$.labs,
$.pabs,
$.let,
$.app,
$.binex,
),
expr : $ => choice(
@ -114,7 +145,7 @@ module.exports = grammar({
axiom : $ => seq(
'axiom',
field('name', $.identifier),
field('name', choice($.identifier, $.symbol)),
repeat($.param_block),
$.ascription,
';'
@ -122,11 +153,11 @@ module.exports = grammar({
definition : $ => seq(
'def',
field('name', $.identifier),
field('name', choice($.identifier, $.symbol)),
repeat($.param_block),
optional($.ascription),
':=',
choice($.expr),
$.expr,
';',
),

147
src/grammar.json generated
View file

@ -25,6 +25,10 @@
{
"type": "SYMBOL",
"name": "variable"
},
{
"type": "SYMBOL",
"name": "fixity"
}
]
}
@ -33,6 +37,10 @@
"type": "PATTERN",
"value": "[a-zA-Z_]\\w*"
},
"symbol": {
"type": "PATTERN",
"value": "[!@#$%^&*-+=<>,./?\\[\\]{}\\\\|`~'\\\"∧∨⊙×≅]+"
},
"comment": {
"type": "TOKEN",
"content": {
@ -74,6 +82,40 @@
}
]
},
"precedence": {
"type": "PATTERN",
"value": "[0-9]+"
},
"fixity": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "infixl"
},
{
"type": "STRING",
"value": "infixr"
}
]
},
{
"type": "SYMBOL",
"name": "precedence"
},
{
"type": "SYMBOL",
"name": "symbol"
},
{
"type": "STRING",
"value": ";"
}
]
},
"variable": {
"type": "SEQ",
"members": [
@ -93,8 +135,45 @@
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "param_block"
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "symbol"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "expr"
}
},
{
"type": "STRING",
"value": ")"
}
]
}
},
{
@ -141,7 +220,7 @@
},
"star": {
"type": "STRING",
"value": "*"
"value": ""
},
"square": {
"type": "CHOICE",
@ -311,6 +390,36 @@
}
]
},
"binex": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "app"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "symbol"
},
{
"type": "SYMBOL",
"name": "binex"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"binding": {
"type": "SEQ",
"members": [
@ -400,18 +509,9 @@
"type": "SYMBOL",
"name": "app_term"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "->"
},
{
"type": "STRING",
"value": "→"
}
]
},
{
"type": "SYMBOL",
@ -437,7 +537,7 @@
},
{
"type": "SYMBOL",
"name": "app"
"name": "binex"
}
]
},
@ -482,8 +582,17 @@
"type": "FIELD",
"name": "name",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "symbol"
}
]
}
},
{
@ -514,8 +623,17 @@
"type": "FIELD",
"name": "name",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "symbol"
}
]
}
},
{
@ -541,14 +659,9 @@
"type": "STRING",
"value": ":="
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expr"
}
]
},
{
"type": "STRING",

95
src/node-types.json generated
View file

@ -23,7 +23,7 @@
"required": true,
"types": [
{
"type": "app",
"type": "binex",
"named": true
},
{
@ -87,6 +87,10 @@
{
"type": "identifier",
"named": true
},
{
"type": "symbol",
"named": true
}
]
}
@ -133,6 +137,29 @@
]
}
},
{
"type": "binex",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "app",
"named": true
},
{
"type": "binex",
"named": true
},
{
"type": "symbol",
"named": true
}
]
}
},
{
"type": "definition",
"named": true,
@ -144,6 +171,10 @@
{
"type": "identifier",
"named": true
},
{
"type": "symbol",
"named": true
}
]
}
@ -186,6 +217,25 @@
]
}
},
{
"type": "fixity",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "precedence",
"named": true
},
{
"type": "symbol",
"named": true
}
]
}
},
{
"type": "labs",
"named": true,
@ -312,6 +362,10 @@
"type": "definition",
"named": true
},
{
"type": "fixity",
"named": true
},
{
"type": "preprocess",
"named": true
@ -396,13 +450,28 @@
{
"type": "variable",
"named": true,
"fields": {},
"fields": {
"type": {
"multiple": true,
"required": true,
"types": [
{
"type": "expr",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "param_block",
"type": "identifier",
"named": true
},
{
"type": "symbol",
"named": true
}
]
@ -420,10 +489,6 @@
"type": ",",
"named": false
},
{
"type": "->",
"named": false
},
{
"type": ":",
"named": false
@ -484,6 +549,14 @@
"type": "in",
"named": false
},
{
"type": "infixl",
"named": false
},
{
"type": "infixr",
"named": false
},
{
"type": "let",
"named": false
@ -492,6 +565,10 @@
"type": "post_command",
"named": true
},
{
"type": "precedence",
"named": true
},
{
"type": "section",
"named": false
@ -500,6 +577,10 @@
"type": "star",
"named": true
},
{
"type": "symbol",
"named": true
},
{
"type": "variable",
"named": false

7780
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -2,67 +2,18 @@
Application
===========
def foo (A B : *) (f : A -> B) (x : A) :=
(fun (x : B) => x) (f x);
def foo := f x;
---
(program
(definition
(identifier)
(param_block
(identifier)
(identifier)
(expr
(app_term
(app
(term
(sort
(star)))))))
(param_block
(identifier)
(expr
(arrow
(app_term
(app
(term
(identifier))))
(expr
(app_term
(app
(term
(identifier))))))))
(param_block
(identifier)
(expr
(app_term
(app
(term
(identifier))))))
(expr
(app_term
(app
(term
(expr
(app_term
(labs
(param_block
(identifier)
(expr
(app_term
(app
(term
(identifier))))))
(expr
(app_term
(app
(term
(identifier)))))))))
(term
(expr
(app_term
(binex
(app
(term
(identifier))
(term
(identifier)))))))))))
(identifier))))))))

View file

@ -2,7 +2,7 @@
Arrows
======
def 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;
---
@ -14,42 +14,48 @@ def foo (A B : *) (f : A -> A -> B) (x : A) := f x x;
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))
(star))))))))
(param_block
(identifier)
(expr
(arrow
(app_term
(binex
(app
(term
(identifier))))
(identifier)))))
(expr
(arrow
(app_term
(binex
(app
(term
(identifier))))
(identifier)))))
(expr
(app_term
(binex
(app
(term
(identifier))))))))))
(identifier)))))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(expr
(app_term
(app
(term
(identifier))
(term
(identifier))
(term
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))
(term
(identifier))
(term
(identifier))))))))

View file

@ -2,7 +2,7 @@
Axioms
======
axiom nat : *;
axiom nat : ;
-----
@ -12,7 +12,8 @@ axiom nat : *;
(ascription
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))))
(star))))))))))

View file

@ -0,0 +1,36 @@
==========
Definition
==========
def foo (A : ★) (x y z : A) := x;
---
(program
(definition
(identifier)
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star))))))))
(param_block
(identifier)
(identifier)
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))))

View file

@ -4,7 +4,7 @@ Include
@include foo.pg
def baz : * := A;
def baz : := A;
@include bar.pg
@ -19,15 +19,17 @@ def baz : * := A;
(ascription
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))
(star))))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(preprocess
(command)
(post_command)))

45
test/corpus/infix.txt Normal file
View file

@ -0,0 +1,45 @@
===============
Infix Operators
===============
infixl 20 +;
infixl 30 *;
infixr 40 ^;
def computation := one + two * four ^ three;
---
(program
(fixity
(precedence)
(symbol))
(fixity
(precedence)
(symbol))
(fixity
(precedence)
(symbol))
(definition
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier)))
(symbol)
(binex
(app
(term
(identifier)))
(symbol)
(binex
(app
(term
(identifier)))
(symbol)
(binex
(app
(term
(identifier)))))))))))

View file

@ -2,7 +2,7 @@
Lambda Abstraction
==================
def foo := fun (A : *) (x : A) : A => x;
def foo := fun (A : ) (x : A) : A => x;
----------
@ -16,25 +16,29 @@ def foo := fun (A : *) (x : A) : A => x;
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))
(star))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(ascription
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))))))
(identifier)))))))))))

View file

@ -23,37 +23,43 @@ def foo :=
(ascription
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(binding
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(binding
(identifier)
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))))))
(identifier)))))))))))

View file

@ -2,7 +2,7 @@
Pi Abstraction
==============
def rel := forall (A : *) (x : A), *;
def rel := forall (A : ★) (x : A), ★;
----------
@ -16,20 +16,23 @@ def rel := forall (A : *) (x : A), *;
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))
(star))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))))))
(star))))))))))))

View file

@ -1,33 +0,0 @@
==========
Definition
==========
def foo (A : *) (x y z : A) := x;
---
(program
(definition
(identifier)
(param_block
(identifier)
(expr
(app_term
(app
(term
(sort
(star)))))))
(param_block
(identifier)
(identifier)
(identifier)
(expr
(app_term
(app
(term
(identifier))))))
(expr
(app_term
(app
(term
(identifier)))))))

View file

@ -3,7 +3,7 @@ Sections
========
section Test
variable (A B C : *);
variable (A B C : );
hypothesis (x : A) (y : B) (z1 z2 : C);
section Nested
@ -18,36 +18,36 @@ end Test
(identifier)
(program
(variable
(param_block
(identifier)
(identifier)
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star))))))))
(variable
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier))))))
(param_block
(identifier)
(identifier)
(expr
(app_term
(binex
(app
(term
(identifier)))))))
@ -60,13 +60,15 @@ end Test
(identifier)
(expr
(app_term
(app
(term
(identifier))))))
(expr
(app_term
(binex
(app
(term
(identifier)))))))
(expr
(app_term
(binex
(app
(term
(identifier))))))))
(identifier)))
(identifier)))

View file

@ -2,7 +2,7 @@
Sorts
=====
def foo (A : *) (B : □) (C : □₁) (D : []) (E : []1) (F : □1) (G : □₁₂₃) := A;
def foo (A : ) (B : □) (C : □₁) (D : []) (E : []1) (F : □1) (G : □₁₂₃) := A;
---
@ -13,60 +13,68 @@ def foo (A : *) (B : □) (C : □₁) (D : []) (E : []1) (F : □1) (G : □₁
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(star)))))))
(star))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(param_block
(identifier)
(expr
(app_term
(binex
(app
(term
(sort
(square)))))))
(square))))))))
(expr
(app_term
(binex
(app
(term
(identifier)))))))
(identifier))))))))