lexer_test.go 7.0 KB

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