lexer_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package parser
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestNextItem(t *testing.T) {
  16. l := &lexer{"Test", "1234", 0, 0, 0, 0, 0, make(chan LexToken)}
  17. r := l.next(1)
  18. if r != '1' {
  19. t.Errorf("Unexpected token: %q", r)
  20. return
  21. }
  22. if r := l.next(0); r != '1' {
  23. t.Errorf("Unexpected token: %q", r)
  24. return
  25. }
  26. if r := l.next(0); r != '2' {
  27. t.Errorf("Unexpected token: %q", r)
  28. return
  29. }
  30. if r := l.next(1); r != '3' {
  31. t.Errorf("Unexpected token: %q", r)
  32. return
  33. }
  34. if r := l.next(2); r != '4' {
  35. t.Errorf("Unexpected token: %q", r)
  36. return
  37. }
  38. if r := l.next(0); r != '3' {
  39. t.Errorf("Unexpected token: %q", r)
  40. return
  41. }
  42. if r := l.next(0); r != '4' {
  43. t.Errorf("Unexpected token: %q", r)
  44. return
  45. }
  46. if r := l.next(0); r != RuneEOF {
  47. t.Errorf("Unexpected token: %q", r)
  48. return
  49. }
  50. }
  51. func TestEquals(t *testing.T) {
  52. l := LexToList("mytest", "not\n test")
  53. if mt := l[0].Type(); mt != "MetaDataGeneral" {
  54. t.Error("Unexpected meta type:", mt)
  55. return
  56. }
  57. if ok, msg := l[0].Equals(l[1], false); ok || msg != `ID is different 53 vs 7
  58. Pos is different 0 vs 5
  59. Val is different not vs test
  60. Identifier is different false vs true
  61. Lline is different 1 vs 2
  62. Lpos is different 1 vs 2
  63. {
  64. "ID": 53,
  65. "Pos": 0,
  66. "Val": "not",
  67. "Identifier": false,
  68. "Lline": 1,
  69. "Lpos": 1
  70. }
  71. vs
  72. {
  73. "ID": 7,
  74. "Pos": 5,
  75. "Val": "test",
  76. "Identifier": true,
  77. "Lline": 2,
  78. "Lpos": 2
  79. }` {
  80. t.Error("Unexpected result:", msg)
  81. return
  82. }
  83. }
  84. func TestBasicTokenLexing(t *testing.T) {
  85. // Test empty string parsing
  86. if res := fmt.Sprint(LexToList("mytest", " \t ")); res != "[EOF]" {
  87. t.Error("Unexpected lexer result:\n ", res)
  88. return
  89. }
  90. // Test arithmetics
  91. input := `name := a + 1 and (ver+x!=1) * 5 > name2`
  92. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  93. `["name" := "a" + v:"1" <AND> ( "ver" + "x" != v:"1" ) * v:"5" > "name2" EOF]` {
  94. t.Error("Unexpected lexer result:\n ", res)
  95. return
  96. }
  97. input = `test := not a * 1.3 or (12 / aa) * 5 DiV 3 % 1 > trUe`
  98. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  99. `["test" := <NOT> "a" * v:"1.3" <OR> ( v:"12" / "aa" ) * v:"5" "DiV" v:"3" % v:"1" > <TRUE> EOF]` {
  100. t.Error("Unexpected lexer result:\n ", res)
  101. return
  102. }
  103. input = `-1.234560e+02+5+2.123 // 1`
  104. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  105. `[- v:"1.234560e+02" + v:"5" + v:"2.123" // v:"1" EOF]` {
  106. t.Error("Unexpected lexer result:\n ", res)
  107. return
  108. }
  109. // Test invalid identifier
  110. input = `5test`
  111. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  112. `[v:"5" "test" EOF]` {
  113. t.Error("Unexpected lexer result:\n ", res)
  114. return
  115. }
  116. input = `@test`
  117. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  118. `[Error: Cannot parse identifier '@test'. Identifies may only contain [a-zA-Z] and [a-zA-Z0-9] from the second character (Line 1, Pos 1) EOF]` {
  119. t.Error("Unexpected lexer result:\n ", res)
  120. return
  121. }
  122. }
  123. func TestAssignmentLexing(t *testing.T) {
  124. input := `name := a + 1`
  125. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  126. `["name" := "a" + v:"1" EOF]` {
  127. t.Error("Unexpected lexer result:", res)
  128. return
  129. }
  130. input = `name := a.a + a.b`
  131. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  132. `["name" := "a" . "a" + "a" . "b" EOF]` {
  133. t.Error("Unexpected lexer result:", res)
  134. return
  135. }
  136. input = `name:=a[1] + b["d"] + c[a]`
  137. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  138. `["name" := "a" [ v:"1" ] + "b" [ "d" ] + "c" [ "a" ] EOF]` {
  139. t.Error("Unexpected lexer result:", res)
  140. return
  141. }
  142. }
  143. func TestBlockLexing(t *testing.T) {
  144. input := `
  145. if a == 1 {
  146. print("xxx")
  147. } elif b > 2 {
  148. print("yyy")
  149. } else {
  150. print("zzz")
  151. }
  152. `
  153. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  154. `[<IF> "a" == v:"1" { "print" ( "xxx" ) } <ELIF> "b" > v:"2" { "print" ( "yyy" ) } <ELSE> { "print" ( "zzz" ) } EOF]` {
  155. t.Error("Unexpected lexer result:", res)
  156. return
  157. }
  158. input = `
  159. for a, b in enum(blist) {
  160. do(a)
  161. }
  162. `
  163. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  164. `[<FOR> "a" , "b" <IN> "enum" ( "blist" ) { "do" ( "a" ) } EOF]` {
  165. t.Error("Unexpected lexer result:", res)
  166. return
  167. }
  168. input = `
  169. for true {
  170. x := "1"
  171. break; continue
  172. }
  173. `
  174. if res := LexToList("mytest", input); fmt.Sprint(res) !=
  175. `[<FOR> <TRUE> { "x" := "1" <BREAK> ; <CONTINUE> } EOF]` {
  176. t.Error("Unexpected lexer result:", res)
  177. return
  178. }
  179. }
  180. func TestStringLexing(t *testing.T) {
  181. // Test unclosed quotes
  182. input := `name "test bla`
  183. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" Error: Unexpected end while reading string value (unclosed quotes) (Line 1, Pos 6) EOF]` {
  184. t.Error("Unexpected lexer result:", res)
  185. return
  186. }
  187. input = `name "test" 'bla'`
  188. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" "test" "bla" EOF]` {
  189. t.Error("Unexpected lexer result:", res)
  190. return
  191. }
  192. input = `name "te
  193. st" 'bla'`
  194. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" Error: invalid syntax while parsing string (Line 1, Pos 6)]` {
  195. t.Error("Unexpected lexer result:", res)
  196. return
  197. }
  198. input = `name r"te
  199. st" 'bla'`
  200. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" "te\n\tst" "bla" EOF]` {
  201. t.Error("Unexpected lexer result:", res)
  202. return
  203. }
  204. // Parsing with escape sequences
  205. input = `"test\n\ttest" '\nfoo\u0028bar' "test{foo}.5w3f"`
  206. if res := LexToList("mytest", input); fmt.Sprint(res) != `["test\n\ttest" "\nfoo(bar" "test{foo}.5w3f" EOF]` {
  207. t.Error("Unexpected lexer result:", res)
  208. return
  209. }
  210. }
  211. func TestCommentLexing(t *testing.T) {
  212. input := `name /* foo
  213. bar
  214. x*/ 'b/* - */la' /*test*/`
  215. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" /* foo
  216. bar
  217. x */ "b/* - */la" /* test */ EOF]` {
  218. t.Error("Unexpected lexer result:", res)
  219. return
  220. }
  221. input = `name /* foo
  222. bar`
  223. if res := LexToList("mytest", input); fmt.Sprint(res) != `["name" Error: Unexpected end while reading comment (Line 1, Pos 8) EOF]` {
  224. t.Error("Unexpected lexer result:", res)
  225. return
  226. }
  227. input = `foo
  228. 1+ 2 # Some comment
  229. bar`
  230. if res := LexToList("mytest", input); fmt.Sprint(res) != `["foo" v:"1" + v:"2" # Some comment
  231. "bar" EOF]` {
  232. t.Error("Unexpected lexer result:", res)
  233. return
  234. }
  235. input = `1+ 2 # Some comment`
  236. if res := LexToList("mytest", input); fmt.Sprint(res) != `[v:"1" + v:"2" # Some comment EOF]` {
  237. t.Error("Unexpected lexer result:", res)
  238. return
  239. }
  240. }
  241. func TestSinkLexing(t *testing.T) {
  242. input := `sink "mysink"
  243. r"
  244. A comment describing the sink.
  245. "
  246. kindmatch [ foo.bar.* ],
  247. scopematch [ "data.read", "data.write" ],
  248. statematch { a : 1, b : NULL },
  249. priority 0,
  250. suppresses [ "myothersink" ]
  251. {
  252. a := 1
  253. }`
  254. if res := LexToList("mytest", input); fmt.Sprint(res) != `[<SINK> "mysink" "\nA comment"... <KINDMATCH> `+
  255. `[ "foo" . "bar" . * ] , <SCOPEMATCH> [ "data.read" , "data.write" ] , <STATEMATCH> `+
  256. `{ "a" : v:"1" , "b" : <NULL> } , <PRIORITY> v:"0" , <SUPPRESSES> [ "myothersink" ] `+
  257. `{ "a" := v:"1" } EOF]` {
  258. t.Error("Unexpected lexer result:", res)
  259. return
  260. }
  261. }