literals.lua 11 KB

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