literals.lua 11 KB

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