Some speed improvements
This commit is contained in:
@@ -26,10 +26,12 @@ obj lexer (Object) {
|
||||
var regs: vector::vector<util::pair<string::string, regex::regex>>
|
||||
var input: string::string
|
||||
var position: int
|
||||
var line_number: int
|
||||
fun construct(): *lexer {
|
||||
regs.construct()
|
||||
input.construct()
|
||||
position = 0
|
||||
line_number = 1
|
||||
return this
|
||||
}
|
||||
fun destruct() {
|
||||
@@ -40,6 +42,7 @@ obj lexer (Object) {
|
||||
regs.copy_construct(&old->regs)
|
||||
input.copy_construct(&old->input)
|
||||
position = old->position
|
||||
line_number = old->line_number
|
||||
}
|
||||
fun operator=(old: lexer) {
|
||||
destruct()
|
||||
@@ -63,16 +66,6 @@ obj lexer (Object) {
|
||||
fun next(): symbol::symbol {
|
||||
if (position >= input.length())
|
||||
return symbol::eof_symbol()
|
||||
/*
|
||||
var max = regs.map(fun(reg_pair: util::pair<string::string,regex::regex>): util::pair<int, string::string> {
|
||||
return util::make_pair(reg_pair.second.long_match(input.slice(position, -1)), reg_pair.first); })
|
||||
.max(fun(first: util::pair<int, string::string>, second: util::pair<int, string::string>): bool
|
||||
{ return first.first < second.first; })
|
||||
if (max.first < 0)
|
||||
return symbol::invalid_symbol()
|
||||
position += max.first
|
||||
return symbol::symbol(max.second, true, input.slice(position-max.first, position))
|
||||
*/
|
||||
var max = -1
|
||||
var max_length = -1
|
||||
for (var i = 0; i < regs.size; i++;) {
|
||||
@@ -84,15 +77,11 @@ obj lexer (Object) {
|
||||
}
|
||||
if (max < 0)
|
||||
return symbol::invalid_symbol()
|
||||
for (var i = position; i < position+max_length; i++;)
|
||||
if (input[i] == '\n')
|
||||
line_number++
|
||||
position += max_length
|
||||
var line_number = fun(str: ref string::string, pos: int): int {
|
||||
var line_no = 1
|
||||
for (var i = 0; i < pos; i++;)
|
||||
if (str[i] == '\n')
|
||||
line_no++
|
||||
return line_no
|
||||
}
|
||||
return symbol::symbol(regs[max].first, true, input.slice(position-max_length, position), line_number(input, position))
|
||||
return symbol::symbol(regs[max].first, true, input.slice(position-max_length, position), line_number)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,7 +188,6 @@ obj regex (Object, Serializable) {
|
||||
return longest
|
||||
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
|
||||
longest = i
|
||||
//next = next.flatten_map<*regexState>(fun(state: *regexState): vector::vector<*regexState> { return state->match_char(to_match[i]); })
|
||||
next = next.flatten_map(fun(state: *regexState): set::set<*regexState> { return state->match_char(to_match[position+i]); })
|
||||
}
|
||||
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
|
||||
|
||||
@@ -21,7 +21,7 @@ fun symbol(nameIn: *char, terminalIn: bool): symbol {
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun symbol(nameIn: string::string, terminalIn: bool): symbol {
|
||||
fun symbol(nameIn: ref string::string, terminalIn: bool): symbol {
|
||||
var toRet.construct(nameIn, terminalIn, string::string("no_value")): symbol
|
||||
return toRet
|
||||
}
|
||||
@@ -31,9 +31,9 @@ fun symbol(nameIn: *char, terminalIn: bool, dataIn: *char): symbol {
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun symbol(nameIn: string::string, terminalIn: bool, dataIn: string::string): symbol return symbol(nameIn, terminalIn, dataIn, 0)
|
||||
fun symbol(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string): symbol return symbol(nameIn, terminalIn, dataIn, 0)
|
||||
|
||||
fun symbol(nameIn: string::string, terminalIn: bool, dataIn: string::string, position: int): symbol {
|
||||
fun symbol(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string, position: int): symbol {
|
||||
var toRet.construct(nameIn, terminalIn, dataIn): symbol
|
||||
toRet.position = position
|
||||
return toRet
|
||||
@@ -55,7 +55,7 @@ obj symbol (Object, Serializable) {
|
||||
position = 0
|
||||
return this
|
||||
}
|
||||
fun construct(nameIn: string::string, terminalIn: bool, dataIn: string::string): *symbol {
|
||||
fun construct(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string): *symbol {
|
||||
name.construct(nameIn)
|
||||
terminal = terminalIn
|
||||
data.construct(dataIn)
|
||||
|
||||
Reference in New Issue
Block a user