2015-10-03 11:32:19 +00:00
|
|
|
package parser
|
2015-10-07 22:38:39 +00:00
|
|
|
|
|
|
|
import "github.com/fatih/hcl/scanner"
|
|
|
|
|
|
|
|
type Parser struct {
|
2015-10-11 21:20:17 +00:00
|
|
|
sc *scanner.Scanner
|
|
|
|
buf struct {
|
|
|
|
tok scanner.Token // last read token
|
|
|
|
n int // buffer size (max = 1)
|
|
|
|
}
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewParser(src []byte) *Parser {
|
|
|
|
return &Parser{
|
2015-10-11 21:20:17 +00:00
|
|
|
sc: scanner.New(src),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) Parse() Node {
|
|
|
|
tok := p.scan()
|
|
|
|
|
|
|
|
switch tok.Type() {
|
|
|
|
case scanner.IDENT:
|
|
|
|
// p.parseStatement()
|
|
|
|
case scanner.EOF:
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
2015-10-11 21:20:17 +00:00
|
|
|
|
|
|
|
return nil
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 21:20:17 +00:00
|
|
|
// scan returns the next token from the underlying scanner.
|
|
|
|
// If a token has been unscanned then read that instead.
|
|
|
|
func (p *Parser) scan() scanner.Token {
|
|
|
|
// If we have a token on the buffer, then return it.
|
|
|
|
if p.buf.n != 0 {
|
|
|
|
p.buf.n = 0
|
|
|
|
return p.buf.tok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise read the next token from the scanner and Save it to the buffer
|
|
|
|
// in case we unscan later.
|
|
|
|
p.buf.tok = p.sc.Scan()
|
|
|
|
|
|
|
|
return p.buf.tok
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
2015-10-11 21:20:17 +00:00
|
|
|
|
|
|
|
// unscan pushes the previously read token back onto the buffer.
|
|
|
|
func (p *Parser) unread() { p.buf.n = 1 }
|