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/parser.go | 28 ++++++++++++++++++++++++++++ conf/scanner.go | 46 +++++++++++++++++++++------------------------- 2 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 conf/parser.go (limited to 'conf') diff --git a/conf/parser.go b/conf/parser.go new file mode 100644 index 0000000..21bc604 --- /dev/null +++ b/conf/parser.go @@ -0,0 +1,28 @@ +package conf + +import ( + "fmt" + "io" + "os" +) + +// Having a Config to Manager function could be nice? +// Or we could just return a Manager from here. +type Config struct { +} + +type parser struct { + scanner +} + +func NewConfig(r io.Reader) *Config { + s := newScanner(r) + tt, tok, err := s.Next() + for err == nil { + fmt.Fprintf(os.Stderr, "tokentype: %d, token: %q, err: %v\n", tt, tok, err) + tt, tok, err = s.Next() + } + fmt.Fprintf(os.Stderr, "Error: tokentype: %d, token: %q, err: %v\n", tt, tok, err) + + return &Config{} +} 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.3