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