summaryrefslogtreecommitdiff
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
parentb0c4b2fbd4098f88b1abc854854fa24c7402c70a (diff)
Implement lexer based on Scanner
We also add the skeleton of a recursive descent parser.
-rw-r--r--conf/parser.go28
-rw-r--r--conf/scanner.go46
-rw-r--r--main.go13
3 files changed, 62 insertions, 25 deletions
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
}
diff --git a/main.go b/main.go
index 23ae701..7a3bb88 100644
--- a/main.go
+++ b/main.go
@@ -1,14 +1,27 @@
package main
import (
+ "bufio"
+ "fmt"
+ "os"
"strings"
+ "github.com/Shugyousha/stasher/conf"
"github.com/Shugyousha/stasher/filter"
"github.com/Shugyousha/stasher/input"
"github.com/Shugyousha/stasher/output"
)
func main() {
+ f, err := os.Open("stasher.cfg")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Could not open config file 'stasher.cfg': %q\n", err)
+ os.Exit(1)
+ }
+
+ c := conf.NewConfig(bufio.NewReader(f))
+ fmt.Fprintf(os.Stderr, "config: %#v\n", c)
+
ffmap := make(map[string]func(string) string, 10)
ffmap["F"] = func(s string) string { return strings.ToUpper(s) }