Last active
July 18, 2021 13:05
-
-
Save erezsh/ab88ad914a81a7b1b2e8c9009ea07ff2 to your computer and use it in GitHub Desktop.
Lark -> Lark.js - A sample of automatic translation
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) | |
stream = lexer_thread.lex(None) | |
if self.options.postlex: | |
return self.options.postlex.process(stream) | |
return stream |
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
lex(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. | |
*/ | |
let lexer; | |
if (!("lexer" in this) || dont_ignore) { | |
lexer = this._build_lexer(dont_ignore); | |
} else { | |
lexer = this.lexer; | |
} | |
let lexer_thread = new LexerThread(lexer, text); | |
let stream = lexer_thread.lex(null); | |
if (this.options.postlex) { | |
return this.options.postlex.process(stream); | |
} | |
return stream; | |
} |
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
class IntParseTable(ParseTable): | |
@classmethod | |
def from_ParseTable(cls, parse_table): | |
enum = list(parse_table.states) | |
state_to_idx = {s:i for i,s in enumerate(enum)} | |
int_states = {} | |
for s, la in parse_table.states.items(): | |
la = {k:(v[0], state_to_idx[v[1]]) if v[0] is Shift else v | |
for k,v in la.items()} | |
int_states[ state_to_idx[s] ] = la | |
start_states = {start:state_to_idx[s] for start, s in parse_table.start_states.items()} | |
end_states = {start:state_to_idx[s] for start, s in parse_table.end_states.items()} | |
return cls(int_states, start_states, end_states) |
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
class IntParseTable extends ParseTable { | |
static from_ParseTable(parse_table) { | |
let cls = this; | |
let la; | |
let enum_ = Array.from(parse_table.states); | |
let state_to_idx = Object.fromEntries( | |
enumerate(enum_).map(([i, s]) => [s, i]) | |
); | |
let int_states = {}; | |
for (let [s, la] of dict_items(parse_table.states)) { | |
la = Object.fromEntries( | |
dict_items(la).map(([k, v]) => [ | |
k, | |
v[0] === Shift ? [v[0], state_to_idx[v[1]]] : v, | |
]) | |
); | |
int_states[state_to_idx[s]] = la; | |
} | |
let start_states = Object.fromEntries( | |
dict_items(parse_table.start_states).map(([start, s]) => [ | |
start, | |
state_to_idx[s], | |
]) | |
); | |
let end_states = Object.fromEntries( | |
dict_items(parse_table.end_states).map(([start, s]) => [ | |
start, | |
state_to_idx[s], | |
]) | |
); | |
return new cls(int_states, start_states, end_states); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment