Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
847 changes: 847 additions & 0 deletions Hare.g4

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions gen-grammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import re
with open("./attributes.tex", "r") as file:
lines = file.read()
r = re.compile(r'\\terminal{(.*?)}')
for i in re.finditer(r, lines):
print(f"\'{i.groups()[0]}\' | ")


5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module lidsol.org/harels

go 1.24.3

require github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
84 changes: 84 additions & 0 deletions harels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"strconv"

"github.com/antlr/antlr4/runtime/Go/antlr"

"lidsol.org/harels/parser"
)

type calcListener struct {
*parser.BaseCalcListener
stack []int
}

func (l *calcListener) push(i int) {
l.stack = append(l.stack, i)
}

func (l *calcListener) pop() int {
if len(l.stack) < 1 {
panic("stack is empty")
}

result := l.stack[len(l.stack)-1]

l.stack = l.stack[:len(l.stack)-1]

return result
}

func (l *calcListener) ExitMulDiv(c *parser.MulDivContext) {
right, left := l.pop(), l.pop()

switch c.GetOp().GetTokenType() {
case parser.CalcParserMUL:
l.push(left * right)
case parser.CalcParserDIV:
l.push(left / right)
default:
panic(fmt.Sprintf("unexpected op: %s", c.GetOp().GetText()))
}
}

func (l *calcListener) ExitAddSub(c *parser.AddSubContext) {
right, left := l.pop(), l.pop()

switch c.GetOp().GetTokenType() {
case parser.CalcParserADD:
l.push(left + right)
case parser.CalcParserSUB:
l.push(left - right)
default:
panic(fmt.Sprintf("unexpected op: %s", c.GetOp().GetText()))
}
}

func (l *calcListener) ExitNumber(c *parser.NumberContext) {
i, err := strconv.Atoi(c.GetText())
if err != nil {
panic(err.Error())
}

l.push(i)
}

func calc(input string) int {
is := antlr.NewInputStream(input)

lexer := parser.NewCalcLexer(is)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)

p := parser.NewCalcParser(stream)

var listener calcListener
antlr.ParseTreeWalkerDefault.Walk(&listener, p.Start())

return listener.pop()
}

func main() {
fmt.Println(calc("1 + 2 * 3"))
}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "harels",
"version": "1.0.0",
"description": "Language Server for Hare Language",
"keywords": [
"Hare",
"language",
"server"
],
"homepage": "https://github.com/LIDSOL/harels#readme",
"bugs": {
"url": "https://github.com/LIDSOL/harels/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/LIDSOL/harels.git"
},
"license": "MIT",
"author": "Lidsol",
"type": "commonjs",
"main": "./client/dist/extension.js",
"scripts": {
"test": "sh ./scripts/e2e.sh"
}
}
25 changes: 25 additions & 0 deletions parser/Calc.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
token literal names:
null
'*'
'/'
'+'
'-'
null
null

token symbolic names:
null
MUL
DIV
ADD
SUB
NUMBER
WHITESPACE

rule names:
start
expression


atn:
[4, 1, 6, 22, 2, 0, 7, 0, 2, 1, 7, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 17, 8, 1, 10, 1, 12, 1, 20, 9, 1, 1, 1, 0, 1, 2, 2, 0, 2, 0, 2, 1, 0, 1, 2, 1, 0, 3, 4, 21, 0, 4, 1, 0, 0, 0, 2, 7, 1, 0, 0, 0, 4, 5, 3, 2, 1, 0, 5, 6, 5, 0, 0, 1, 6, 1, 1, 0, 0, 0, 7, 8, 6, 1, -1, 0, 8, 9, 5, 5, 0, 0, 9, 18, 1, 0, 0, 0, 10, 11, 10, 3, 0, 0, 11, 12, 7, 0, 0, 0, 12, 17, 3, 2, 1, 4, 13, 14, 10, 2, 0, 0, 14, 15, 7, 1, 0, 0, 15, 17, 3, 2, 1, 3, 16, 10, 1, 0, 0, 0, 16, 13, 1, 0, 0, 0, 17, 20, 1, 0, 0, 0, 18, 16, 1, 0, 0, 0, 18, 19, 1, 0, 0, 0, 19, 3, 1, 0, 0, 0, 20, 18, 1, 0, 0, 0, 2, 16, 18]
10 changes: 10 additions & 0 deletions parser/Calc.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MUL=1
DIV=2
ADD=3
SUB=4
NUMBER=5
WHITESPACE=6
'*'=1
'/'=2
'+'=3
'-'=4
35 changes: 35 additions & 0 deletions parser/CalcLexer.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
token literal names:
null
'*'
'/'
'+'
'-'
null
null

token symbolic names:
null
MUL
DIV
ADD
SUB
NUMBER
WHITESPACE

rule names:
MUL
DIV
ADD
SUB
NUMBER
WHITESPACE

channel names:
DEFAULT_TOKEN_CHANNEL
HIDDEN

mode names:
DEFAULT_MODE

atn:
[4, 0, 6, 33, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 4, 4, 23, 8, 4, 11, 4, 12, 4, 24, 1, 5, 4, 5, 28, 8, 5, 11, 5, 12, 5, 29, 1, 5, 1, 5, 0, 0, 6, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 1, 0, 2, 1, 0, 48, 57, 3, 0, 9, 10, 13, 13, 32, 32, 34, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 1, 13, 1, 0, 0, 0, 3, 15, 1, 0, 0, 0, 5, 17, 1, 0, 0, 0, 7, 19, 1, 0, 0, 0, 9, 22, 1, 0, 0, 0, 11, 27, 1, 0, 0, 0, 13, 14, 5, 42, 0, 0, 14, 2, 1, 0, 0, 0, 15, 16, 5, 47, 0, 0, 16, 4, 1, 0, 0, 0, 17, 18, 5, 43, 0, 0, 18, 6, 1, 0, 0, 0, 19, 20, 5, 45, 0, 0, 20, 8, 1, 0, 0, 0, 21, 23, 7, 0, 0, 0, 22, 21, 1, 0, 0, 0, 23, 24, 1, 0, 0, 0, 24, 22, 1, 0, 0, 0, 24, 25, 1, 0, 0, 0, 25, 10, 1, 0, 0, 0, 26, 28, 7, 1, 0, 0, 27, 26, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 27, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 32, 6, 5, 0, 0, 32, 12, 1, 0, 0, 0, 3, 0, 24, 29, 1, 6, 0, 0]
10 changes: 10 additions & 0 deletions parser/CalcLexer.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MUL=1
DIV=2
ADD=3
SUB=4
NUMBER=5
WHITESPACE=6
'*'=1
'/'=2
'+'=3
'-'=4
46 changes: 46 additions & 0 deletions parser/calc_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions parser/calc_lexer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading