literals.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. print('testing scanner')
  2. debug = require "debug"
  3. local function dostring (x) return assert(load(x))() end
  4. dostring("x \v\f = \t\r 'a\0a' \v\f\f")
  5. assert(x == 'a\0a' and string.len(x) == 3)
  6. -- escape sequences
  7. assert('\n\"\'\\' == [[
  8. "'\]])
  9. assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
  10. -- assume ASCII just for tests:
  11. assert("\09912" == 'c12')
  12. assert("\99ab" == 'cab')
  13. assert("\099" == '\99')
  14. assert("\099\n" == 'c\10')
  15. assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
  16. assert(010 .. 020 .. -030 == "1020-30")
  17. -- hexadecimal escapes
  18. assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")
  19. local function lexstring (x, y, n)
  20. local f = assert(load('return '..x..', debug.getinfo(1).currentline'))
  21. local s, l = f()
  22. assert(s == y and l == n)
  23. end
  24. lexstring("'abc\\z \n efg'", "abcefg", 2)
  25. lexstring("'abc\\z \n\n\n'", "abc", 4)
  26. lexstring("'\\z \n\t\f\v\n'", "", 3)
  27. lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5)
  28. lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5)
  29. lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4)
  30. lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4)
  31. lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2)
  32. assert("abc\z
  33. def\z
  34. ghi\z
  35. " == 'abcdefghi')
  36. -- Error in escape sequences
  37. local function lexerror (s, err)
  38. local st, msg = load('return '..s)
  39. if err ~= '<eof>' then err = "'"..err.."'" end
  40. assert(not st and string.find(msg, "near "..err, 1, true))
  41. end
  42. lexerror([["abc\x"]], [[\x"]])
  43. lexerror([["abc\x]], [[\x]])
  44. lexerror([["\x]], [[\x]])
  45. lexerror([["\x5"]], [[\x5"]])
  46. lexerror([["\x5]], [[\x5]])
  47. lexerror([["\xr"]], [[\xr]])
  48. lexerror([["\xr]], [[\xr]])
  49. lexerror([["\x.]], [[\x.]])
  50. lexerror([["\x8%"]], [[\x8%]])
  51. lexerror([["\xAG]], [[\xAG]])
  52. lexerror([["\g"]], [[\g]])
  53. lexerror([["\g]], [[\g]])
  54. lexerror([["\."]], [[\.]])
  55. lexerror([["\999"]], [[\999]])
  56. lexerror([["xyz\300"]], [[\300]])
  57. lexerror([[" \256"]], [[\256]])
  58. -- unfinished strings
  59. lexerror("[=[alo]]", "<eof>")
  60. lexerror("[=[alo]=", "<eof>")
  61. lexerror("[=[alo]", "<eof>")
  62. lexerror("'alo", "<eof>")
  63. lexerror("'alo \\z \n\n", "<eof>")
  64. lexerror("'alo \\z", "<eof>")
  65. lexerror([['alo \98]], "<eof>")
  66. -- valid characters in variable names
  67. for i = 0, 255 do
  68. local s = string.char(i)
  69. assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1"))
  70. assert(not string.find(s, "[a-zA-Z_0-9]") ==
  71. not load("a" .. s .. "1 = 1"))
  72. end
  73. -- long variable names
  74. var = string.rep('a', 15000)
  75. prog = string.format("%s = 5", var)
  76. dostring(prog)
  77. assert(_G[var] == 5)
  78. var = nil
  79. print('+')
  80. -- escapes --
  81. assert("\n\t" == [[
  82. ]])
  83. assert([[
  84. $debug]] == "\n $debug")
  85. assert([[ [ ]] ~= [[ ] ]])
  86. -- long strings --
  87. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  88. assert(string.len(b) == 960)
  89. prog = [=[
  90. print('+')
  91. a1 = [["isto e' um string com várias 'aspas'"]]
  92. a2 = "'aspas'"
  93. assert(string.find(a1, a2) == 31)
  94. print('+')
  95. a1 = [==[temp = [[um valor qualquer]]; ]==]
  96. assert(load(a1))()
  97. assert(temp == 'um valor qualquer')
  98. -- long strings --
  99. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  100. assert(string.len(b) == 960)
  101. print('+')
  102. a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
  103. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  104. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  105. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  106. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  107. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  108. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  109. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  110. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  111. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  112. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  113. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  114. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  115. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  116. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  117. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  118. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  119. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  120. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  121. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  122. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  123. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  124. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  125. ]]
  126. assert(string.len(a) == 1863)
  127. assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
  128. x = 1
  129. ]=]
  130. print('+')
  131. x = nil
  132. dostring(prog)
  133. assert(x)
  134. prog = nil
  135. a = nil
  136. b = nil
  137. -- testing line ends
  138. prog = [[
  139. a = 1 -- a comment
  140. b = 2
  141. x = [=[
  142. hi
  143. ]=]
  144. y = "\
  145. hello\r\n\
  146. "
  147. return debug.getinfo(1).currentline
  148. ]]
  149. for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
  150. local prog, nn = string.gsub(prog, "\n", n)
  151. assert(dostring(prog) == nn)
  152. assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
  153. end
  154. -- testing comments and strings with long brackets
  155. a = [==[]=]==]
  156. assert(a == "]=")
  157. a = [==[[===[[=[]]=][====[]]===]===]==]
  158. assert(a == "[===[[=[]]=][====[]]===]===")
  159. a = [====[[===[[=[]]=][====[]]===]===]====]
  160. assert(a == "[===[[=[]]=][====[]]===]===")
  161. a = [=[]]]]]]]]]=]
  162. assert(a == "]]]]]]]]")
  163. --[===[
  164. x y z [==[ blu foo
  165. ]==
  166. ]
  167. ]=]==]
  168. error error]=]===]
  169. -- generate all strings of four of these chars
  170. local x = {"=", "[", "]", "\n"}
  171. local len = 4
  172. local function gen (c, n)
  173. if n==0 then coroutine.yield(c)
  174. else
  175. for _, a in pairs(x) do
  176. gen(c..a, n-1)
  177. end
  178. end
  179. end
  180. for s in coroutine.wrap(function () gen("", len) end) do
  181. assert(s == load("return [====[\n"..s.."]====]")())
  182. end
  183. -- testing decimal point locale
  184. if os.setlocale("pt_BR") or os.setlocale("ptb") then
  185. assert(not load("á = 3")) -- parser still works with C locale
  186. assert(not load("a = (3,4)"))
  187. assert(tonumber("3,4") == 3.4 and tonumber"3.4" == nil)
  188. assert(assert(load("return 3.4"))() == 3.4)
  189. assert(assert(load("return .4,3"))() == .4)
  190. assert(assert(load("return 4."))() == 4.)
  191. assert(assert(load("return 4.+.5"))() == 4.5)
  192. local a,b = load("return 4.5.")
  193. assert(string.find(b, "'4%.5%.'"))
  194. assert(os.setlocale("C"))
  195. else
  196. (Message or print)(
  197. '\a\n >>> pt_BR locale not available: skipping decimal point tests <<<\n\a')
  198. end
  199. -- testing %q x line ends
  200. local s = "a string with \r and \n and \r\n and \n\r"
  201. local c = string.format("return %q", s)
  202. assert(assert(load(c))() == s)
  203. -- testing errors
  204. assert(not load"a = 'non-ending string")
  205. assert(not load"a = 'non-ending string\n'")
  206. assert(not load"a = '\\345'")
  207. assert(not load"a = [=x]")
  208. print('OK')