forked from jasder/antlr
LexerActionExecutor
This commit is contained in:
parent
db3eb7edcb
commit
f3db2d9637
|
@ -26,6 +26,10 @@ func (la *LexerAction) InitLexerAction(action int){
|
||||||
la.isPositionDependent = false
|
la.isPositionDependent = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *LexerAction) execute(lexer *Lexer) {
|
||||||
|
panic("Not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
func (this *LexerAction) hashString() string {
|
func (this *LexerAction) hashString() string {
|
||||||
return "" + this.actionType
|
return "" + this.actionType
|
||||||
}
|
}
|
||||||
|
@ -34,7 +38,6 @@ func (this *LexerAction) equals(other *LexerAction) {
|
||||||
return this == other
|
return this == other
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Implements the {@code skip} lexer action by calling {@link Lexer//skip}.
|
// Implements the {@code skip} lexer action by calling {@link Lexer//skip}.
|
||||||
//
|
//
|
||||||
|
@ -375,7 +378,7 @@ func NewLexerIndexedCustomAction(offset int, action *LexerAction) *LexerIndexedC
|
||||||
// using the provided {@code lexer}.</p>
|
// using the provided {@code lexer}.</p>
|
||||||
func (this *LexerIndexedCustomAction) execute(lexer *Lexer) {
|
func (this *LexerIndexedCustomAction) execute(lexer *Lexer) {
|
||||||
// assume the input stream position was properly set by the calling code
|
// assume the input stream position was properly set by the calling code
|
||||||
this.action.execute(lexer *Lexer)
|
this.action.execute(lexer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *LexerIndexedCustomAction) hashString() string {
|
func (this *LexerIndexedCustomAction) hashString() string {
|
||||||
|
|
|
@ -7,14 +7,31 @@ package antlr4
|
||||||
// efficiently, ensuring that actions appearing only at the end of the rule do
|
// efficiently, ensuring that actions appearing only at the end of the rule do
|
||||||
// not cause bloating of the {@link DFA} created for the lexer.</p>
|
// not cause bloating of the {@link DFA} created for the lexer.</p>
|
||||||
|
|
||||||
//var LexerIndexedCustomAction = require('./LexerAction').LexerIndexedCustomAction
|
type LexerActionExecutor struct {
|
||||||
|
lexerActions []*LexerAction
|
||||||
|
hashString string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLexerActionExecutor(lexerActions []*LexerAction) *LexerActionExecutor {
|
||||||
|
|
||||||
|
if (lexerActions == nil){
|
||||||
|
lexerActions = make([]*LexerAction)
|
||||||
|
}
|
||||||
|
|
||||||
|
this := new(LexerActionExecutor)
|
||||||
|
|
||||||
|
this.lexerActions = lexerActions
|
||||||
|
|
||||||
func LexerActionExecutor(lexerActions) {
|
|
||||||
this.lexerActions = lexerActions == nil ? [] : lexerActions
|
|
||||||
// Caches the result of {@link //hashCode} since the hash code is an element
|
// Caches the result of {@link //hashCode} since the hash code is an element
|
||||||
// of the performance-critical {@link LexerATNConfig//hashCode} operation.
|
// of the performance-critical {@link LexerATNConfig//hashCode} operation.
|
||||||
this.hashString = lexerActions.toString() // "".join([str(la) for la in
|
|
||||||
// lexerActions]))
|
var s string
|
||||||
|
for _, a := range lexerActions {
|
||||||
|
s += a.hashString()
|
||||||
|
}
|
||||||
|
|
||||||
|
this.hashString = s // "".join([str(la) for la in
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,11 +48,14 @@ func LexerActionExecutor(lexerActions) {
|
||||||
//
|
//
|
||||||
// @return A {@link LexerActionExecutor} for executing the combine actions
|
// @return A {@link LexerActionExecutor} for executing the combine actions
|
||||||
// of {@code lexerActionExecutor} and {@code lexerAction}.
|
// of {@code lexerActionExecutor} and {@code lexerAction}.
|
||||||
LexerActionExecutor.append = function(lexerActionExecutor, lexerAction) {
|
func LexerActionExecutorappend(lexerActionExecutor *LexerActionExecutor, lexerAction *LexerAction) *LexerActionExecutor {
|
||||||
if (lexerActionExecutor == nil) {
|
if (lexerActionExecutor == nil) {
|
||||||
return NewLexerActionExecutor([ lexerAction ])
|
return NewLexerActionExecutor([]*LexerAction{lexerAction})
|
||||||
}
|
}
|
||||||
var lexerActions = lexerActionExecutor.lexerActions.concat([ lexerAction ])
|
|
||||||
|
var lexerActions = append(lexerActionExecutor.lexerActions, lexerAction )
|
||||||
|
|
||||||
|
// var lexerActions = lexerActionExecutor.lexerActions.concat([ lexerAction ])
|
||||||
return NewLexerActionExecutor(lexerActions)
|
return NewLexerActionExecutor(lexerActions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,16 +87,20 @@ LexerActionExecutor.append = function(lexerActionExecutor, lexerAction) {
|
||||||
// @return A {@link LexerActionExecutor} which stores input stream offsets
|
// @return A {@link LexerActionExecutor} which stores input stream offsets
|
||||||
// for all position-dependent lexer actions.
|
// for all position-dependent lexer actions.
|
||||||
// /
|
// /
|
||||||
func (this *LexerActionExecutor) fixOffsetBeforeMatch(offset) {
|
func (this *LexerActionExecutor) fixOffsetBeforeMatch(offset int) *LexerActionExecutor {
|
||||||
var updatedLexerActions = nil
|
var updatedLexerActions []*LexerAction = nil
|
||||||
for i := 0; i < len(this.lexerActions); i++ {
|
for i := 0; i < len(this.lexerActions); i++ {
|
||||||
if (this.lexerActions[i].isPositionDependent &&
|
_, ok := this.lexerActions[i].(*LexerIndexedCustomAction)
|
||||||
!(this.lexerActions[i] instanceof LexerIndexedCustomAction)) {
|
if (this.lexerActions[i].isPositionDependent && !ok){
|
||||||
if (updatedLexerActions == nil) {
|
if (updatedLexerActions == nil) {
|
||||||
updatedLexerActions = this.lexerActions.concat([])
|
updatedLexerActions = make([]*LexerAction)
|
||||||
|
|
||||||
|
for _,a:= range this.lexerActions {
|
||||||
|
updatedLexerActions = append(updatedLexerActions, a)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
updatedLexerActions[i] = NewLexerIndexedCustomAction(offset,
|
|
||||||
this.lexerActions[i])
|
updatedLexerActions[i] = NewLexerIndexedCustomAction(offset, this.lexerActions[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (updatedLexerActions == nil) {
|
if (updatedLexerActions == nil) {
|
||||||
|
@ -104,27 +128,28 @@ func (this *LexerActionExecutor) fixOffsetBeforeMatch(offset) {
|
||||||
// {@link IntStream//seek} to set the {@code input} position to the beginning
|
// {@link IntStream//seek} to set the {@code input} position to the beginning
|
||||||
// of the token.
|
// of the token.
|
||||||
// /
|
// /
|
||||||
func (this *LexerActionExecutor) execute(lexer, input, startIndex) {
|
func (this *LexerActionExecutor) execute(lexer *Lexer, input *InputStream, startIndex int) {
|
||||||
var requiresSeek = false
|
var requiresSeek = false
|
||||||
var stopIndex = input.index
|
var stopIndex = input.index
|
||||||
try {
|
|
||||||
for i := 0; i < len(this.lexerActions); i++ {
|
defer func(){
|
||||||
var lexerAction = this.lexerActions[i]
|
|
||||||
if _, ok := lexerAction.(LexerIndexedCustomAction); ok {
|
|
||||||
var offset = lexerAction.offset
|
|
||||||
input.seek(startIndex + offset)
|
|
||||||
lexerAction = lexerAction.action
|
|
||||||
requiresSeek = (startIndex + offset) != stopIndex
|
|
||||||
} else if (lexerAction.isPositionDependent) {
|
|
||||||
input.seek(stopIndex)
|
|
||||||
requiresSeek = false
|
|
||||||
}
|
|
||||||
lexerAction.execute(lexer)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (requiresSeek) {
|
if (requiresSeek) {
|
||||||
input.seek(stopIndex)
|
input.seek(stopIndex)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for i := 0; i < len(this.lexerActions); i++ {
|
||||||
|
var lexerAction *LexerAction = this.lexerActions[i]
|
||||||
|
if la, ok := lexerAction.(*LexerIndexedCustomAction); ok {
|
||||||
|
var offset = la.offset
|
||||||
|
input.seek(startIndex + offset)
|
||||||
|
lexerAction = la.action
|
||||||
|
requiresSeek = (startIndex + offset) != stopIndex
|
||||||
|
} else if (lexerAction.isPositionDependent) {
|
||||||
|
input.seek(stopIndex)
|
||||||
|
requiresSeek = false
|
||||||
|
}
|
||||||
|
lexerAction.execute(lexer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,14 +157,14 @@ func (this *LexerActionExecutor) hashString() {
|
||||||
return this.hashString
|
return this.hashString
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *LexerActionExecutor) equals(other) {
|
func (this *LexerActionExecutor) equals(other interface{}) bool {
|
||||||
if (this == other) {
|
if (this == other) {
|
||||||
return true
|
return true
|
||||||
} else if (!_, ok := other.(LexerActionExecutor); ok) {
|
} else if _, ok := other.(*LexerActionExecutor); !ok {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
return this.hashString == other.hashString &&
|
return this.hashString == other.(*LexerActionExecutor).hashString &&
|
||||||
this.lexerActions == other.lexerActions
|
this.lexerActions == other.(*LexerActionExecutor).lexerActions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,6 @@ import (
|
||||||
// {@link SemanticContext} within the scope of this outer class.</p>
|
// {@link SemanticContext} within the scope of this outer class.</p>
|
||||||
//
|
//
|
||||||
|
|
||||||
//var Set = require('./../Utils').Set
|
|
||||||
|
|
||||||
type SemanticContext struct {
|
type SemanticContext struct {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue