Fix state cleanup bug between rule/config parser runs.

This fixes a bug that has been annoying me minorly for some time now:
sometimes, after parse errors, a subsequent parser run would fail.  The reason
is that yylex() modifies some global variables (yytext, yydata) during its run
to keep state. To make subsequent parser runs correct, these have to be reset
before each run.

Also, close files after reading them.
This commit is contained in:
Julius Volz 2013-01-12 02:35:40 +01:00
parent ab746d068e
commit cb6eb30182
2 changed files with 7 additions and 1 deletions

View File

@ -35,6 +35,8 @@ func LoadFromReader(configReader io.Reader) (*Config, error) {
yyin = configReader
yypos = 1
yyline = 1
yydata = ""
yytext = ""
lexer := &ConfigLexer{}
yyParse(lexer)
@ -54,6 +56,7 @@ func LoadFromString(configString string) (*Config, error) {
func LoadFromFile(fileName string) (*Config, error) {
configReader, err := os.Open(fileName)
defer configReader.Close()
if err != nil {
return &Config{}, err
}

View File

@ -48,9 +48,10 @@ func LoadFromReader(rulesReader io.Reader, singleExpr bool) (interface{}, error)
yyin = rulesReader
yypos = 1
yyline = 1
yydata = ""
yytext = ""
lexer := &RulesLexer{
parsedRules: []*Rule{},
startToken: START_RULES,
}
@ -91,6 +92,7 @@ func LoadRulesFromString(rulesString string) ([]*Rule, error) {
func LoadRulesFromFile(fileName string) ([]*Rule, error) {
rulesReader, err := os.Open(fileName)
defer rulesReader.Close()
if err != nil {
return []*Rule{}, err
}
@ -112,6 +114,7 @@ func LoadExprFromString(exprString string) (ast.Node, error) {
func LoadExprFromFile(fileName string) (ast.Node, error) {
exprReader, err := os.Open(fileName)
defer exprReader.Close()
if err != nil {
return nil, err
}