go(lex): Fix compilation errors.
authorsgf <sgf.dma@gmail.com>
Fri, 24 Jun 2022 12:34:48 +0000 (15:34 +0300)
committersgf <sgf.dma@gmail.com>
Fri, 24 Jun 2022 15:38:30 +0000 (18:38 +0300)
lexical-scanning-in-go/go.mod [new file with mode: 0644]
lexical-scanning-in-go/lex.go

diff --git a/lexical-scanning-in-go/go.mod b/lexical-scanning-in-go/go.mod
new file mode 100644 (file)
index 0000000..e102dcd
--- /dev/null
@@ -0,0 +1,3 @@
+module lex
+
+go 1.17
index a106b54..8e047d6 100644 (file)
@@ -1,4 +1,13 @@
 
+package main
+
+import (
+    "fmt"
+    "strings"
+    "unicode/utf8"
+    "unicode"
+)
+
 type item struct {
     typ itemType;
     val string
@@ -11,9 +20,12 @@ const (
     itemDot
     itemEOF
     itemNumber
-}
+    itemText
+    itemLeftMeta
+    itemRightMeta
+)
 
-func (i item) String() {
+func (i item) String() string {
     switch i.typ {
     case itemEOF:
         return "EOF"
@@ -29,8 +41,8 @@ func (i item) String() {
 type stateFn func(*lexer) stateFn;
 
 func (l *lexer) run() {
-    for state := lexText; state != nil; }
-        state = state(lexer)
+    for state := lexText; state != nil; {
+        state = state(l)
     }
     close(l.items)
 }
@@ -61,6 +73,7 @@ func (l *lexer) emit(t itemType) {
 
 const leftMeta = "{{"
 const rightMeta = "}}"
+const eof = utf8.RuneError
 
 func lexText (l *lexer) stateFn {
     for {
@@ -85,6 +98,20 @@ func lexLeftMeta(l *lexer) stateFn {
     return lexInsideAction
 }
 
+func lexRightMeta(l *lexer) stateFn {
+    l.pos += len(rightMeta)
+    l.emit(itemRightMeta)
+    return lexText
+}
+
+func isSpace(r rune) bool {
+    return r < unicode.MaxASCII && unicode.IsSpace(r)
+}
+
+func isAlphaNumeric(r rune) bool {
+    return r < unicode.MaxASCII && unicode.IsLetter(r) || unicode.IsNumber(r)
+}
+
 func lexInsideAction(l *lexer) stateFn {
     for {
         if strings.HasPrefix(l.input[l.pos:], rightMeta) {
@@ -95,28 +122,26 @@ func lexInsideAction(l *lexer) stateFn {
             return l.errorf("unclosed action")
         case isSpace(r):
             l.ignore()
-        case r == '|':
-            l.emit(itemPipe)
-        case r == '"':
-            return lexQuote
         case r == '+' || r == '-' || '0' <= r && r <= '9':
             l.backup()
             return lexNumber
+            /*
         case isAlphaNumeric(r):
             l.backup()
             return lexIdentifier
+            */
         }
     }
 }
 
-func (l *lexer) next() (rune int) {
+func (l *lexer) next() (r rune) {
     if l.pos >= len(l.input) {
         l.width = 0
         return eof
     }
-    run, l.width = utf8.DecodeRuneInString(l.input[l.poas:])
+    r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
     l.pos += l.width
-    return rune
+    return r
 }
 
 func (l *lexer) ignore() {
@@ -127,10 +152,10 @@ func (l *lexer) backup() {
     l.pos -= l.width
 }
 
-func (l *lexer) peek() int {
-    rune := l.next()
+func (l *lexer) peek() rune {
+    r := l.next()
     l.backup()
-    return rune
+    return r
 }
 
 func (l *lexer) accept(valid string) bool {
@@ -171,10 +196,14 @@ func lexNumber (l *lexer) stateFn {
 }
 
 func (l *lexer) errorf(format string, args ...interface{}) stateFn {
-    stateFn {
-        l.items <- item{
-            itemError,
-            fmt.Sprintf(format, args...),
-        }
-        return nil
+    l.items <- item {
+        itemError,
+        fmt.Sprintf(format, args...),
+    }
+    return nil
 }
+
+func main() {
+    fmt.Println("Huita")
+}
+