literals.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. -- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $
  2. -- See Copyright Notice in file all.lua
  3. print('testing scanner')
  4. local debug = require "debug"
  5. local function dostring (x) return assert(load(x), "")() end
  6. dostring("x \v\f = \t\r 'a\0a' \v\f\f")
  7. assert(x == 'a\0a' and string.len(x) == 3)
  8. -- escape sequences
  9. assert('\n\"\'\\' == [[
  10. "'\]])
  11. assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
  12. -- assume ASCII just for tests:
  13. assert("\09912" == 'c12')
  14. assert("\99ab" == 'cab')
  15. assert("\099" == '\99')
  16. assert("\099\n" == 'c\10')
  17. assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
  18. assert(010 .. 020 .. -030 == "1020-30")
  19. -- hexadecimal escapes
  20. assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")
  21. local function lexstring (x, y, n)
  22. local f = assert(load('return ' .. x ..
  23. ', require"debug".getinfo(1).currentline', ''))
  24. local s, l = f()
  25. assert(s == y and l == n)
  26. end
  27. lexstring("'abc\\z \n efg'", "abcefg", 2)
  28. lexstring("'abc\\z \n\n\n'", "abc", 4)
  29. lexstring("'\\z \n\t\f\v\n'", "", 3)
  30. lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5)
  31. lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5)
  32. lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4)
  33. lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4)
  34. lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2)
  35. assert("abc\z
  36. def\z
  37. ghi\z
  38. " == 'abcdefghi')
  39. -- UTF-8 sequences
  40. assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
  41. -- limits for 1-byte sequences
  42. assert("\u{0}\u{7F}" == "\x00\z\x7F")
  43. -- limits for 2-byte sequences
  44. assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF")
  45. -- limits for 3-byte sequences
  46. assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF")
  47. -- limits for 4-byte sequences
  48. assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF")
  49. -- Error in escape sequences
  50. local function lexerror (s, err)
  51. local st, msg = load('return ' .. s, '')
  52. if err ~= '<eof>' then err = err .. "'" end
  53. assert(not st and string.find(msg, "near .-" .. err))
  54. end
  55. lexerror([["abc\x"]], [[\x"]])
  56. lexerror([["abc\x]], [[\x]])
  57. lexerror([["\x]], [[\x]])
  58. lexerror([["\x5"]], [[\x5"]])
  59. lexerror([["\x5]], [[\x5]])
  60. lexerror([["\xr"]], [[\xr]])
  61. lexerror([["\xr]], [[\xr]])
  62. lexerror([["\x.]], [[\x.]])
  63. lexerror([["\x8%"]], [[\x8%%]])
  64. lexerror([["\xAG]], [[\xAG]])
  65. lexerror([["\g"]], [[\g]])
  66. lexerror([["\g]], [[\g]])
  67. lexerror([["\."]], [[\%.]])
  68. lexerror([["\999"]], [[\999"]])
  69. lexerror([["xyz\300"]], [[\300"]])
  70. lexerror([[" \256"]], [[\256"]])
  71. -- errors in UTF-8 sequences
  72. lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large
  73. lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{'
  74. lexerror([["abc\u"]], [[abc\u"]]) -- missing '{'
  75. lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}'
  76. lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}'
  77. lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}'
  78. lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits
  79. -- unfinished strings
  80. lexerror("[=[alo]]", "<eof>")
  81. lexerror("[=[alo]=", "<eof>")
  82. lexerror("[=[alo]", "<eof>")
  83. lexerror("'alo", "<eof>")
  84. lexerror("'alo \\z \n\n", "<eof>")
  85. lexerror("'alo \\z", "<eof>")
  86. lexerror([['alo \98]], "<eof>")
  87. -- valid characters in variable names
  88. for i = 0, 255 do
  89. local s = string.char(i)
  90. assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", ""))
  91. assert(not string.find(s, "[a-zA-Z_0-9]") ==
  92. not load("a" .. s .. "1 = 1", ""))
  93. end
  94. -- long variable names
  95. var1 = string.rep('a', 15000) .. '1'
  96. var2 = string.rep('a', 15000) .. '2'
  97. prog = string.format([[
  98. %s = 5
  99. %s = %s + 1
  100. return function () return %s - %s end
  101. ]], var1, var2, var1, var1, var2)
  102. local f = dostring(prog)
  103. assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
  104. var1, var2, f = nil
  105. print('+')
  106. -- escapes --
  107. assert("\n\t" == [[
  108. ]])
  109. assert([[
  110. $debug]] == "\n $debug")
  111. assert([[ [ ]] ~= [[ ] ]])
  112. -- long strings --
  113. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  114. assert(string.len(b) == 960)
  115. prog = [=[
  116. print('+')
  117. a1 = [["this is a 'string' with several 'quotes'"]]
  118. a2 = "'quotes'"
  119. assert(string.find(a1, a2) == 34)
  120. print('+')
  121. a1 = [==[temp = [[an arbitrary value]]; ]==]
  122. assert(load(a1))()
  123. assert(temp == 'an arbitrary value')
  124. -- long strings --
  125. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  126. assert(string.len(b) == 960)
  127. print('+')
  128. a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
  129. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  130. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  131. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  132. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  133. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  134. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  135. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  136. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  137. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  138. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  139. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  140. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  141. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  142. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  143. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  144. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  145. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  146. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  147. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  148. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  149. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  150. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  151. ]]
  152. assert(string.len(a) == 1863)
  153. assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
  154. x = 1
  155. ]=]
  156. print('+')
  157. x = nil
  158. dostring(prog)
  159. assert(x)
  160. prog = nil
  161. a = nil
  162. b = nil
  163. -- testing line ends
  164. prog = [[
  165. a = 1 -- a comment
  166. b = 2
  167. x = [=[
  168. hi
  169. ]=]
  170. y = "\
  171. hello\r\n\
  172. "
  173. return require"debug".getinfo(1).currentline
  174. ]]
  175. for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
  176. local prog, nn = string.gsub(prog, "\n", n)
  177. assert(dostring(prog) == nn)
  178. assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
  179. end
  180. -- testing comments and strings with long brackets
  181. a = [==[]=]==]
  182. assert(a == "]=")
  183. a = [==[[===[[=[]]=][====[]]===]===]==]
  184. assert(a == "[===[[=[]]=][====[]]===]===")
  185. a = [====[[===[[=[]]=][====[]]===]===]====]
  186. assert(a == "[===[[=[]]=][====[]]===]===")
  187. a = [=[]]]]]]]]]=]
  188. assert(a == "]]]]]]]]")
  189. --[===[
  190. x y z [==[ blu foo
  191. ]==
  192. ]
  193. ]=]==]
  194. error error]=]===]
  195. -- generate all strings of four of these chars
  196. local x = {"=", "[", "]", "\n"}
  197. local len = 4
  198. local function gen (c, n)
  199. if n==0 then coroutine.yield(c)
  200. else
  201. for _, a in pairs(x) do
  202. gen(c..a, n-1)
  203. end
  204. end
  205. end
  206. for s in coroutine.wrap(function () gen("", len) end) do
  207. assert(s == load("return [====[\n"..s.."]====]", "")())
  208. end
  209. -- testing decimal point locale
  210. if os.setlocale("pt_BR") or os.setlocale("ptb") then
  211. assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4)
  212. assert(tonumber(" -.4 ") == -0.4)
  213. assert(tonumber(" +0x.41 ") == 0X0.41)
  214. assert(not load("a = (3,4)"))
  215. assert(assert(load("return 3.4"))() == 3.4)
  216. assert(assert(load("return .4,3"))() == .4)
  217. assert(assert(load("return 4."))() == 4.)
  218. assert(assert(load("return 4.+.5"))() == 4.5)
  219. assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
  220. assert(tonumber"inf" == nil and tonumber"NAN" == nil)
  221. assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
  222. local a,b = load("return 4.5.")
  223. assert(string.find(b, "'4%.5%.'"))
  224. assert(os.setlocale("C"))
  225. else
  226. (Message or print)(
  227. '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n')
  228. end
  229. -- testing %q x line ends
  230. local s = "a string with \r and \n and \r\n and \n\r"
  231. local c = string.format("return %q", s)
  232. assert(assert(load(c))() == s)
  233. -- testing errors
  234. assert(not load"a = 'non-ending string")
  235. assert(not load"a = 'non-ending string\n'")
  236. assert(not load"a = '\\345'")
  237. assert(not load"a = [=x]")
  238. print('OK')