This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lark import Lark | |
grammar = r""" | |
start: _NL* section+ | |
section: HEADER content* | |
HEADER: "--- SECTION ---" _NL | |
content: number_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
?start: (statement | _NL) * | |
?statement: simple_statement _NL | |
| print_statement _NL | |
?simple_statement: assign | expr | |
assign: NAME "=" sum | |
?expr: sum | |
print_statement: "print" expr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(start, end) { | |
if (end === undefined) { | |
end = start; | |
start = 0; | |
} | |
res = [] | |
for (let i=start; i<end; i++) res.push(i); | |
return res | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://github.com/j2kun/tex-convert-delimiters/blob/master/convert-delimiters.py | |
# A response to https://jeremykun.com/2019/04/20/a-working-mathematicians-guide-to-parsing/ | |
import sys | |
from lark import Lark, Transformer | |
lark = Lark(r''' | |
tex: (mathmode_offset | mathmode_inline | TEXT)+ | |
mathmode_offset: OFFSETDOLLAR TEXT+ OFFSETDOLLAR | OFFSETOPEN TEXT+ OFFSETCLOSE |
NewerOlder