Skip to content

Instantly share code, notes, and snippets.

View erezsh's full-sized avatar

Erez Shinan erezsh

View GitHub Profile
@erezsh
erezsh / JSONLexer.py
Created December 13, 2021 08:54
Parsers used for Lark benchmarks
# Generated from JSON.g4 by ANTLR 4.7.1
# encoding: utf-8
from __future__ import print_function
from antlr4 import *
from io import StringIO
import sys
def serializedATN():
with StringIO() as buf:
function downloadToFile(content, filename, contentType) {
const a = document.createElement('a');
const file = new Blob([content], {type: contentType});
a.href= URL.createObjectURL(file);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
};
@erezsh
erezsh / vanon1.py
Last active September 1, 2021 18:17
LALR try for vanon (1)
from lark import Lark
grammar = r"""
start: _NL* section+
section: HEADER content*
HEADER: "--- SECTION ---" _NL
content: number_list
let tree = parser.parse('{"c": ["a", "b",2]}')
// Object-based transformer
let OT = lark.Transformer.fromObj({
array: (args) => args,
pair: (args) => args,
number: ([x]) => parseInt(x.value),
string: ([x]) => x.value.slice(1, -1),
object: (entries) => Object.fromEntries(entries)
})
@erezsh
erezsh / 0_LexMethod.py
Last active July 18, 2021 13:05
Lark -> Lark.js - A sample of automatic translation
def lex(self, text, dont_ignore=False):
"""Only lex (and postlex) the text, without parsing it. Only relevant when lexer='standard'
When dont_ignore=True, the lexer will return all tokens, even those marked for %ignore.
"""
if not hasattr(self, 'lexer') or dont_ignore:
lexer = self._build_lexer(dont_ignore)
else:
lexer = self.lexer
lexer_thread = LexerThread(lexer, text)
?start: (statement | _NL) *
?statement: simple_statement _NL
| print_statement _NL
?simple_statement: assign | expr
assign: NAME "=" sum
?expr: sum
print_statement: "print" expr
@erezsh
erezsh / lark.js
Last active July 1, 2021 15:33
WIP of Lark js (far from working yet)
function range(start, end) {
if (end === undefined) {
end = start;
start = 0;
}
res = []
for (let i=start; i<end; i++) res.push(i);
return res
};
@erezsh
erezsh / context.py
Created June 16, 2021 17:48
Thread-safe context object for creating a layered context scope.
import threading
from contextlib import contextmanager
class Context(threading.local):
def __init__(self):
self._ctx = [{}]
def __getattr__(self, name):
for scope in reversed(self._ctx):
if name in scope:
from casts import def_cast, cast
# Define cast from tuple to list
@def_cast(auto=True)
def cast_to(i: tuple, cls: list):
return cls(i)
...
class SortedList(list):
@erezsh
erezsh / gist:6f0ae0cc3541d3bed106ae735aa06ee8
Last active August 17, 2020 20:53
Design idea for Omnidoc (working title)

Entities

TreeType

Nodes in the graph implement the interface TreeType{set of tree-nodes supported}

Examples of TreeType: