summaryrefslogtreecommitdiff
path: root/conf/scanner.go
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-12-25 14:45:13 +0100
committerSilvan Jegen <s.jegen@gmail.com>2016-12-25 14:45:13 +0100
commitbe3749d9f77e1410c11d0e8b7158a13e51faa41c (patch)
treed511852b1af04c740b29e41133a1f47de7e54f7c /conf/scanner.go
parentb0c4b2fbd4098f88b1abc854854fa24c7402c70a (diff)
Implement lexer based on Scanner
We also add the skeleton of a recursive descent parser.
Diffstat (limited to 'conf/scanner.go')
-rw-r--r--conf/scanner.go46
1 files changed, 21 insertions, 25 deletions
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
}