Browse Source

remove underscore names in parser

pull/16/head
Brett Langdon 11 years ago
parent
commit
533135c9f9
1 changed files with 55 additions and 55 deletions
  1. +55
    -55
      parser.go

+ 55
- 55
parser.go View File

@ -12,63 +12,63 @@ import (
) )
type Parser struct { type Parser struct {
settings *Section
scanner *Scanner
cur_tok token.Token
cur_section *Section
previous []*Section
settings *Section
scanner *Scanner
curTok token.Token
curSection *Section
previous []*Section
} }
func NewParser(reader io.Reader) *Parser { func NewParser(reader io.Reader) *Parser {
settings := NewSection() settings := NewSection()
return &Parser{ return &Parser{
scanner: NewScanner(reader),
settings: settings,
cur_section: settings,
previous: make([]*Section, 0),
scanner: NewScanner(reader),
settings: settings,
curSection: settings,
previous: make([]*Section, 0),
} }
} }
func (this *Parser) syntaxError(msg string) error { func (this *Parser) syntaxError(msg string) error {
msg = fmt.Sprintf( msg = fmt.Sprintf(
"Syntax error line <%d> column <%d>: %s", "Syntax error line <%d> column <%d>: %s",
this.cur_tok.Line,
this.cur_tok.Column,
this.curTok.Line,
this.curTok.Column,
msg, msg,
) )
return errors.New(msg) return errors.New(msg)
} }
func (this *Parser) readToken() token.Token { func (this *Parser) readToken() token.Token {
this.cur_tok = this.scanner.NextToken()
return this.cur_tok
this.curTok = this.scanner.NextToken()
return this.curTok
} }
func (this *Parser) parseReference(starting_section *Section, period bool) (Value, error) {
func (this *Parser) parseReference(startingSection *Section, period bool) (Value, error) {
name := "" name := ""
if period == false { if period == false {
name = this.cur_tok.Literal
name = this.curTok.Literal
} }
for { for {
this.readToken() this.readToken()
if this.cur_tok.ID == token.PERIOD && period == false {
if this.curTok.ID == token.PERIOD && period == false {
period = true period = true
} else if period && this.cur_tok.ID == token.IDENTIFIER {
} else if period && this.curTok.ID == token.IDENTIFIER {
if len(name) > 0 { if len(name) > 0 {
name += "." name += "."
} }
name += this.cur_tok.Literal
name += this.curTok.Literal
period = false period = false
} else if this.cur_tok.ID == token.SEMICOLON {
} else if this.curTok.ID == token.SEMICOLON {
break break
} else { } else {
msg := fmt.Sprintf("expected ';' instead found '%s'", this.cur_tok.Literal)
msg := fmt.Sprintf("expected ';' instead found '%s'", this.curTok.Literal)
return nil, this.syntaxError(msg) return nil, this.syntaxError(msg)
} }
} }
if len(name) == 0 { if len(name) == 0 {
return nil, this.syntaxError( return nil, this.syntaxError(
fmt.Sprintf("expected IDENTIFIER instead found %s", this.cur_tok.Literal),
fmt.Sprintf("expected IDENTIFIER instead found %s", this.curTok.Literal),
) )
} }
@ -76,7 +76,7 @@ func (this *Parser) parseReference(starting_section *Section, period bool) (Valu
return nil, this.syntaxError(fmt.Sprintf("expected IDENTIFIER after PERIOD")) return nil, this.syntaxError(fmt.Sprintf("expected IDENTIFIER after PERIOD"))
} }
value, err := starting_section.Resolve(name)
value, err := startingSection.Resolve(name)
if err != nil { if err != nil {
err = errors.New("Reference error, " + err.Error()) err = errors.New("Reference error, " + err.Error())
} }
@ -88,31 +88,31 @@ func (this *Parser) parseSetting(name string) error {
this.readToken() this.readToken()
read_next := true read_next := true
switch this.cur_tok.ID {
switch this.curTok.ID {
case token.STRING: case token.STRING:
value = NewString(this.cur_tok.Literal)
value = NewString(this.curTok.Literal)
case token.BOOLEAN: case token.BOOLEAN:
bool_val, err := strconv.ParseBool(this.cur_tok.Literal)
boolVal, err := strconv.ParseBool(this.curTok.Literal)
if err != nil { if err != nil {
return nil return nil
} }
value = NewBoolean(bool_val)
value = NewBoolean(boolVal)
case token.NULL: case token.NULL:
value = NewNull() value = NewNull()
case token.INTEGER: case token.INTEGER:
int_val, err := strconv.ParseInt(this.cur_tok.Literal, 10, 64)
intVal, err := strconv.ParseInt(this.curTok.Literal, 10, 64)
if err != nil { if err != nil {
return err return err
} }
value = NewInteger(int_val)
value = NewInteger(intVal)
case token.FLOAT: case token.FLOAT:
float_val, err := strconv.ParseFloat(this.cur_tok.Literal, 64)
floatVal, err := strconv.ParseFloat(this.curTok.Literal, 64)
if err != nil { if err != nil {
return err return err
} }
value = NewFloat(float_val)
value = NewFloat(floatVal)
case token.PERIOD: case token.PERIOD:
reference, err := this.parseReference(this.cur_section, true)
reference, err := this.parseReference(this.curSection, true)
if err != nil { if err != nil {
return err return err
} }
@ -127,33 +127,33 @@ func (this *Parser) parseSetting(name string) error {
read_next = false read_next = false
default: default:
return this.syntaxError( return this.syntaxError(
fmt.Sprintf("expected STRING, INTEGER, FLOAT, BOOLEAN or IDENTIFIER, instead found %s", this.cur_tok.ID),
fmt.Sprintf("expected STRING, INTEGER, FLOAT, BOOLEAN or IDENTIFIER, instead found %s", this.curTok.ID),
) )
} }
if read_next { if read_next {
this.readToken() this.readToken()
} }
if this.cur_tok.ID != token.SEMICOLON {
msg := fmt.Sprintf("expected ';' instead found '%s'", this.cur_tok.Literal)
if this.curTok.ID != token.SEMICOLON {
msg := fmt.Sprintf("expected ';' instead found '%s'", this.curTok.Literal)
return this.syntaxError(msg) return this.syntaxError(msg)
} }
this.readToken() this.readToken()
this.cur_section.Set(name, value)
this.curSection.Set(name, value)
return nil return nil
} }
func (this *Parser) parseInclude() error { func (this *Parser) parseInclude() error {
if this.cur_tok.ID != token.STRING {
msg := fmt.Sprintf("expected STRING instead found '%s'", this.cur_tok.ID)
if this.curTok.ID != token.STRING {
msg := fmt.Sprintf("expected STRING instead found '%s'", this.curTok.ID)
return this.syntaxError(msg) return this.syntaxError(msg)
} }
pattern := this.cur_tok.Literal
pattern := this.curTok.Literal
this.readToken() this.readToken()
if this.cur_tok.ID != token.SEMICOLON {
msg := fmt.Sprintf("expected ';' instead found '%s'", this.cur_tok.Literal)
if this.curTok.ID != token.SEMICOLON {
msg := fmt.Sprintf("expected ';' instead found '%s'", this.curTok.Literal)
return this.syntaxError(msg) return this.syntaxError(msg)
} }
@ -161,25 +161,25 @@ func (this *Parser) parseInclude() error {
if err != nil { if err != nil {
return err return err
} }
old_scanner := this.scanner
oldScanner := this.scanner
for _, filename := range filenames { for _, filename := range filenames {
reader, err := os.Open(filename) reader, err := os.Open(filename)
if err != nil { if err != nil {
return err return err
} }
// this.cur_section.AddInclude(filename)
// this.curSection.AddInclude(filename)
this.scanner = NewScanner(reader) this.scanner = NewScanner(reader)
this.parse() this.parse()
} }
this.scanner = old_scanner
this.scanner = oldScanner
this.readToken() this.readToken()
return nil return nil
} }
func (this *Parser) parseSection(name string) error { func (this *Parser) parseSection(name string) error {
section := this.cur_section.AddSection(name)
this.previous = append(this.previous, this.cur_section)
this.cur_section = section
section := this.curSection.AddSection(name)
this.previous = append(this.previous, this.curSection)
this.curSection = section
return nil return nil
} }
@ -188,10 +188,10 @@ func (this *Parser) endSection() error {
return this.syntaxError("unexpected section end '}'") return this.syntaxError("unexpected section end '}'")
} }
p_len := len(this.previous)
previous := this.previous[p_len-1]
this.previous = this.previous[0 : p_len-1]
this.cur_section = previous
pLen := len(this.previous)
previous := this.previous[pLen-1]
this.previous = this.previous[0 : pLen-1]
this.curSection = previous
return nil return nil
} }
@ -202,24 +202,24 @@ func (this *Parser) GetSettings() *Section {
func (this *Parser) parse() error { func (this *Parser) parse() error {
this.readToken() this.readToken()
for { for {
if this.cur_tok.ID == token.EOF {
if this.curTok.ID == token.EOF {
break break
} }
tok := this.cur_tok
tok := this.curTok
this.readToken() this.readToken()
switch tok.ID { switch tok.ID {
case token.COMMENT: case token.COMMENT:
// this.cur_section.AddComment(tok.Literal)
// this.curSection.AddComment(tok.Literal)
case token.INCLUDE: case token.INCLUDE:
this.parseInclude() this.parseInclude()
case token.IDENTIFIER: case token.IDENTIFIER:
if this.cur_tok.ID == token.LBRACKET {
if this.curTok.ID == token.LBRACKET {
err := this.parseSection(tok.Literal) err := this.parseSection(tok.Literal)
if err != nil { if err != nil {
return err return err
} }
this.readToken() this.readToken()
} else if this.cur_tok.ID == token.EQUAL {
} else if this.curTok.ID == token.EQUAL {
err := this.parseSetting(tok.Literal) err := this.parseSetting(tok.Literal)
if err != nil { if err != nil {
return err return err


Loading…
Cancel
Save