Repository: goplus/gop
Pull request #2151 (2025-03-01 16:17:50)

tpl/token: $ -> => <>


Patch:
diff --git a/demo/gop-parser/pseudo-2/main.gop b/demo/gop-parser/pseudo-2/main.gop
index f7b67a0e..7c9ee963 100644
--- a/demo/gop-parser/pseudo-2/main.gop
+++ b/demo/gop-parser/pseudo-2/main.gop
@@ -31,7 +31,7 @@ untilStmt = "REPEAT" ";" doc "UNTIL" expr
 
 typeExpr = "INTEGER" | "REAL" | "STRING" | "BOOLEAN"
 
-expr = term2Expr % ("<" | "<=" | ">" | ">=" | "=" | "!=")
+expr = term2Expr % ("<" | "<=" | ">" | ">=" | "=" | "<>")
 
 term2Expr = term1Expr % ("+" | "-")
 
diff --git a/scanner/scanner.go b/scanner/scanner.go
index 3842de77..1994eb8c 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -994,7 +994,7 @@ scanAgain:
 			} else if s.ch == '>' { // <>
 				s.next()
 				tok = token.BIDIARROW
-			} else { // << <<=
+			} else { // < <= << <<=
 				tok = s.switch4(token.LSS, token.LEQ, '<', token.SHL, token.SHL_ASSIGN)
 			}
 		case '>':
diff --git a/tpl/scanner/scanner.go b/tpl/scanner/scanner.go
index de65b44c..16087781 100644
--- a/tpl/scanner/scanner.go
+++ b/tpl/scanner/scanner.go
@@ -702,9 +702,14 @@ scanAgain:
 				insertSemi = true
 			}
 		case '-':
-			t.Tok = s.switch3(token.SUB, token.SUB_ASSIGN, '-', token.DEC)
-			if t.Tok == token.DEC {
-				insertSemi = true
+			if s.ch == '>' { // ->
+				s.next()
+				t.Tok = token.SRARROW
+			} else { // -- -=
+				t.Tok = s.switch3(token.SUB, token.SUB_ASSIGN, '-', token.DEC)
+				if t.Tok == token.DEC {
+					insertSemi = true
+				}
 			}
 		case '*':
 			t.Tok = s.switch2(token.MUL, token.MUL_ASSIGN)
@@ -751,16 +756,19 @@ scanAgain:
 		case '^':
 			t.Tok = s.switch2(token.XOR, token.XOR_ASSIGN)
 		case '<':
-			if s.ch == '-' {
+			if s.ch == '-' { // <-
 				s.next()
 				t.Tok = token.ARROW
-			} else {
+			} else if s.ch == '>' { // <>
+				s.next()
+				t.Tok = token.BIDIARROW
+			} else { // <= << <<=
 				t.Tok = s.switch4(token.LT, token.LE, '<', token.SHL, token.SHL_ASSIGN)
 			}
 		case '>':
 			t.Tok = s.switch4(token.GT, token.GE, '>', token.SHR, token.SHR_ASSIGN)
 		case '=':
-			t.Tok = s.switch2(token.ASSIGN, token.EQ)
+			t.Tok = s.switch3(token.ASSIGN, token.EQ, '>', token.DRARROW)
 		case '!':
 			t.Tok = s.switch2(token.NOT, token.NE)
 		case '&':
@@ -774,6 +782,8 @@ scanAgain:
 			t.Tok = s.switch3(token.OR, token.OR_ASSIGN, '|', token.LOR)
 		case '?':
 			t.Tok = token.QUESTION
+		case '$':
+			t.Tok = token.ENV
 		case '~':
 			t.Tok = token.TILDE
 		case '@':
diff --git a/tpl/token/token.go b/tpl/token/token.go
index 55909117..e03f3112 100644
--- a/tpl/token/token.go
+++ b/tpl/token/token.go
@@ -87,6 +87,7 @@ const (
 	QUESTION  = '?'
 	TILDE     = '~'
 	AT        = '@'
+	ENV       = '$'
 )
 
 const (
@@ -122,6 +123,10 @@ const (
 	DEFINE   // :=
 	ELLIPSIS // ...
 
+	DRARROW   // =>
+	SRARROW   // ->
+	BIDIARROW // <>
+
 	operator_end
 )
 
@@ -169,6 +174,7 @@ var tokens = [...]string{
 	QUESTION:  "?",
 	TILDE:     "~",
 	AT:        "@",
+	ENV:       "$",
 
 	SHL:     "<<",
 	SHR:     ">>",
@@ -199,6 +205,10 @@ var tokens = [...]string{
 	GE:       ">=",
 	DEFINE:   ":=",
 	ELLIPSIS: "...",
+
+	DRARROW:   "=>",
+	SRARROW:   "->",
+	BIDIARROW: "<>",
 }
 
 const (


Test patch:
diff --git a/tpl/token/token_test.go b/tpl/token/token_test.go
index 8854afc4..a275f14f 100644
--- a/tpl/token/token_test.go
+++ b/tpl/token/token_test.go
@@ -38,7 +38,7 @@ func TestToken(t *testing.T) {
 		count++
 		return 0
 	})
-	if count != 25 {
+	if count != 28 {
 		t.Fatal("ForEach:", count)
 	}
 }