
org.textmapper.tool.templates.go_token.ltp Maven / Gradle / Ivy
${template main-}
${file 'token.go'-}
${call go.header-}
${call token-}
${end-}
${end}
${template token-}
package ${self->go.package()}
import (
"fmt"
)
// Token is an enum of all terminal symbols of the ${opts.lang} language.
type Token int
// Token values.
const (
UNAVAILABLE Token = iota-1
EOI
${for i in [1, syntax.terminals - 1]-}
${syntax.symbols[i]->tokenName()}${if syntax.symbols[i].isConstant()} // ${util.escape(syntax.symbols[i].getConstantValue())}${end}
${end-}
NumTokens
)
var tokenStr = [...]string{
"EOI",
${for i in [1, syntax.terminals - 1]-}
"${util.escape(syntax.symbols[i].isConstant() ? util.escape(syntax.symbols[i].getConstantValue()) : syntax.symbols[i].id)}",
${end-}
}
func (tok Token) String() string {
if tok >= 0 && int(tok) < len(tokenStr) {
return tokenStr[tok]
}
return fmt.Sprintf("token(%d)", tok)
}
${end}
${cached query tokenName() = self.id->go.escapeGoReserved()}
${cached query tokenNodeName() = self.name.CamelCase}
${cached query invalidTokenName() = syntax.symbols.select(s|s.nameText == 'invalid_token' && s.isTerm()).first()->tokenName()}
${cached query hasInvalidToken() = syntax.symbols.exists(s|s.nameText == 'invalid_token' && s.isTerm())}
${cached query isSpace(tok) = tok.nameText == 'invalid_token' || self->spaceTokens().exists(it| it == tok)}
${cached query spaceTokens() = syntax.lexerRules.select(x|x.kindAsText == 'space').collectUnique(it | it.symbol)}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy