From be3749d9f77e1410c11d0e8b7158a13e51faa41c Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sun, 25 Dec 2016 14:45:13 +0100 Subject: Implement lexer based on Scanner We also add the skeleton of a recursive descent parser. --- conf/scanner.go | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'conf/scanner.go') diff --git a/conf/scanner.go b/conf/scanner.go index fd2a3cf..172103f 100644 --- a/conf/scanner.go +++ b/conf/scanner.go @@ -5,62 +5,58 @@ import ( "io" ) -type token int +type tokentype int const ( - Name token = iota - Delimiter token = iota - Nothing token = iota - IfStatement token = iota + Name tokentype = iota + Literal + Delimiter + Nothing + IfStatement ) -// Having a Config to Manager function could be nice? -// Or we could just return a Manager from here. -type Config struct { +type token struct { + Type tokentype } type scanner struct { r io.Reader - ss bufio.Scanner + bs bufio.Scanner prev token cur token } -func NewConfig(r io.Reader) *Config { - br := bufio.NewReader(r) - s := newScanner(br) - s.next() - - return &Config{} -} - func newScanner(r io.Reader) *scanner { ns := bufio.NewScanner(r) ns.Split(bufio.ScanWords) sc := scanner{ r: r, - ss: *ns, + bs: *ns, } return &sc } -func getTokenType(s string) token { +func getTokenType(s string) tokentype { return Nothing } -func (s *scanner) next() (token, string, error) { - stop := s.ss.Scan() - if stop { - return Nothing, "", s.ss.Err() +func (s *scanner) Next() (tokentype, string, error) { + more := s.bs.Scan() + if !more { + err := s.bs.Err() + if err != nil { + return Nothing, "", err + } + return Nothing, "", io.EOF } - tokstr := s.ss.Text() + tokstr := s.bs.Text() token := getTokenType(tokstr) return token, tokstr, nil } -func (s *scanner) peek() (token, string, error) { +func (s *scanner) peek() (tokentype, string, error) { return Nothing, "", nil } -- cgit v1.2.1-18-gbd029