Nodes in the graph implement the interface TreeType{set of tree-nodes supported}
Examples of TreeType:
| # 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); | |
| }; |
| 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) | |
| }) |
| 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 |
| function range(start, end) { | |
| if (end === undefined) { | |
| end = start; | |
| start = 0; | |
| } | |
| res = [] | |
| for (let i=start; i<end; i++) res.push(i); | |
| return res | |
| }; |
| 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): |