aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/llex.lua
blob: 077f1aafda7fd191389872c55f7c99e03649ce83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
--[[--------------------------------------------------------------------

  llex.lua
  Lua 5 lexical analyzer in Lua
  This file is part of Yueliang.

  Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- Notes:
-- * intended to 'imitate' llex.c code; performance is not a concern
-- * tokens are strings; code structure largely retained
-- * deleted stuff (compared to llex.c) are noted, comments retained
-- * Added:
--   luaX:chunkid (from lobject.c)
-- * To use the lexer:
--   (1) luaX:init() to initialize the lexer
--   (2) luaX:setinput() to set the input stream to lex, get LS
--   (3) call luaX:lex() to get tokens, until "TK_EOS":
--       LS.t.token = luaX:lex(LS, LS.t)
-- * since EOZ is returned as a string, be careful when regexp testing
----------------------------------------------------------------------]]

luaX = {}

-- FIRST_RESERVED is not required as tokens are manipulated as strings
-- TOKEN_LEN deleted; maximum length of a reserved word

------------------------------------------------------------------------
-- "ORDER RESERVED" deleted; enumeration in one place: luaX.RESERVED
------------------------------------------------------------------------

-- terminal symbols denoted by reserved words: TK_AND to TK_WHILE
-- other terminal symbols: TK_NAME to TK_EOS
luaX.RESERVED = [[
TK_AND and
TK_BREAK break
TK_DO do
TK_ELSE else
TK_ELSEIF elseif
TK_END end
TK_FALSE false
TK_FOR for
TK_FUNCTION function
TK_IF if
TK_IN in
TK_LOCAL local
TK_NIL nil
TK_NOT not
TK_OR or
TK_REPEAT repeat
TK_RETURN return
TK_THEN then
TK_TRUE true
TK_UNTIL until
TK_WHILE while
TK_NAME *name
TK_CONCAT ..
TK_DOTS ...
TK_EQ ==
TK_GE >=
TK_LE <=
TK_NE ~=
TK_NUMBER *number
TK_STRING *string
TK_EOS <eof>]]

-- NUM_RESERVED is not required; number of reserved words

--[[--------------------------------------------------------------------
-- Instead of passing seminfo, the Token struct (e.g. LS.t) is passed
-- so that lexer functions can use its table element, LS.t.seminfo
--
-- Token (struct of LS.t and LS.lookahead):
--   token  -- token symbol
--   seminfo  -- semantics information
--
-- LexState (struct of LS; LS is initialized by luaX:setinput):
--   current  -- current character
--   linenumber  -- input line counter
--   lastline  -- line of last token 'consumed'
--   t  -- current token (table: struct Token)
--   lookahead  -- look ahead token (table: struct Token)
--   fs  -- 'FuncState' is private to the parser
--   L -- LuaState
--   z  -- input stream
--   buff  -- buffer for tokens
--   source  -- current source name
--   nestlevel  -- level of nested non-terminals
----------------------------------------------------------------------]]

-- token2string is now a hash; see luaX:init

------------------------------------------------------------------------
-- initialize lexer
------------------------------------------------------------------------
function luaX:init()
  self.token2string = {}
  self.string2token = {}
  for v in string.gfind(self.RESERVED, "[^\n]+") do
    local _, _, tok, str = string.find(v, "(%S+)%s+(%S+)")
    self.token2string[tok] = str
    self.string2token[str] = tok
  end
end

luaX.MAXSRC = 80

------------------------------------------------------------------------
-- returns a suitably-formatted chunk name or id
-- * from lobject.c, used in llex.c and ldebug.c
-- * the result, out, is returned (was first argument)
------------------------------------------------------------------------
function luaX:chunkid(source, bufflen)
  local out
  local first = string.sub(source, 1, 1)
  if first == "=" then
    out = string.sub(source, 2, bufflen)  -- remove first char
  else  -- out = "source", or "...source"
    if first == "@" then
      source = string.sub(source, 2)  -- skip the '@'
      bufflen = bufflen - string.len(" `...' ")
      local l = string.len(source)
      out = ""
      if l > bufflen then
        source = string.sub(source, 1 + l - bufflen)  -- get last part of file name
        out = out.."..."
      end
      out = out..source
    else  -- out = [string "string"]
      local len = string.find(source, "\n", 1, 1)  -- stop at first newline
      len = len and (len - 1) or string.len(source)
      bufflen = bufflen - string.len(" [string \"...\"] ")
      if len > bufflen then len = bufflen end
      out = "[string \""
      if len < string.len(source) then  -- must truncate?
        out = out..string.sub(source, 1, len).."..."
      else
        out = out..source
      end
      out = out.."\"]"
    end
  end
  return out
end

--[[--------------------------------------------------------------------
-- Support functions for lexer
-- * all lexer errors eventually reaches errorline:
     checklimit -> syntaxerror -> error -> errorline
                      lexerror -> error -> errorline
----------------------------------------------------------------------]]

------------------------------------------------------------------------
-- limit check, syntax error if fails (also called by parser)
------------------------------------------------------------------------
function luaX:checklimit(ls, val, limit, msg)
  if val > limit then
    msg = string.format("too many %s (limit=%d)", msg, limit)
    self:syntaxerror(ls, msg)
  end
end

------------------------------------------------------------------------
-- formats error message and throws error (also called by parser)
------------------------------------------------------------------------
function luaX:errorline(ls, s, token, line)
  local buff = self:chunkid(ls.source, self.MAXSRC)
  error(string.format("%s:%d: %s near `%s'", buff, line, s, token))
end

------------------------------------------------------------------------
-- throws an error, adds line number
------------------------------------------------------------------------
function luaX:error(ls, s, token)
  self:errorline(ls, s, token, ls.linenumber)
end

------------------------------------------------------------------------
-- throws a syntax error (mainly called by parser)
-- * ls.t.token has to be set by the function calling luaX:lex
--   (see next() and lookahead() in lparser.c)
------------------------------------------------------------------------
function luaX:syntaxerror(ls, msg)
  local lasttoken
  local tok = ls.t.token
  if tok == "TK_NAME" then
    lasttoken = ls.t.seminfo
  elseif tok == "TK_STRING" or tok == "TK_NUMBER" then
    lasttoken = ls.buff
  else
    lasttoken = self:token2str(ls.t.token)
  end
  self:error(ls, msg, lasttoken)
end

------------------------------------------------------------------------
-- look up token and return keyword if found (also called by parser)
------------------------------------------------------------------------
function luaX:token2str(ls, token)
  if string.sub(token, 1, 3) ~= "TK_" then
    return token
  else
    --lua_assert(string.len(token) == 1)
    return self.token2string[token]
  end
end

------------------------------------------------------------------------
-- throws a lexer error
------------------------------------------------------------------------
function luaX:lexerror(ls, s, token)
  if token == "TK_EOS" then
    self:error(ls, s, self:token2str(ls, token))
  else
    self:error(ls, s, ls.buff)
  end
end

------------------------------------------------------------------------
-- move on to next line
------------------------------------------------------------------------
function luaX:inclinenumber(LS)
  self:next(LS)  -- skip '\n'
  LS.linenumber = LS.linenumber + 1
  self:checklimit(LS, LS.linenumber, self.MAX_INT, "lines in a chunk")
end

luaX.MAX_INT = 2147483645  -- INT_MAX-2 for 32-bit systems (llimits.h)

------------------------------------------------------------------------
-- initializes an input stream for lexing
-- * if LS (the lexer state) is passed as a table, then it is filled in,
--   otherwise it has to be retrieved as a return value
------------------------------------------------------------------------
function luaX:setinput(L, LS, z, source)
  if not LS then LS = {} end  -- create struct
  if not LS.lookahead then LS.lookahead = {} end
  if not LS.t then LS.t = {} end
  LS.L = L
  LS.lookahead.token = "TK_EOS"  -- no look-ahead token
  LS.z = z
  LS.fs = nil
  LS.linenumber = 1
  LS.lastline = 1
  LS.source = source
  self:next(LS)  -- read first char
  if LS.current == "#" then
    repeat  -- skip first line
      self:next(LS)
    until LS.current == "\n" or LS.current == "EOZ"
  end
  return LS
end

--[[--------------------------------------------------------------------
-- LEXICAL ANALYZER
----------------------------------------------------------------------]]

-- NOTE the following buffer handling stuff are no longer required:
-- use buffer to store names, literal strings and numbers
-- EXTRABUFF deleted; extra space to allocate when growing buffer
-- MAXNOCHECK deleted; maximum number of chars that can be read without checking buffer size
-- checkbuffer(LS, len)	deleted

------------------------------------------------------------------------
-- gets the next character and returns it
------------------------------------------------------------------------
function luaX:next(LS)
  local c = luaZ:zgetc(LS.z)
  LS.current = c
  return c
end

------------------------------------------------------------------------
-- saves the given character into the token buffer
------------------------------------------------------------------------
function luaX:save(LS, c)
  LS.buff = LS.buff..c
end

------------------------------------------------------------------------
-- save current character into token buffer, grabs next character
------------------------------------------------------------------------
function luaX:save_and_next(LS)
  self:save(LS, LS.current)
  return self:next(LS)
end

------------------------------------------------------------------------
-- reads a name
-- * originally returns the string length
------------------------------------------------------------------------
function luaX:readname(LS)
  LS.buff = ""
  repeat
    self:save_and_next(LS)
  until LS.current == "EOZ" or not string.find(LS.current, "[_%w]")
  return LS.buff
end

------------------------------------------------------------------------
-- reads a number (LUA_NUMBER)
------------------------------------------------------------------------
function luaX:read_numeral(LS, comma, Token)
  LS.buff = ""
  if comma then self:save(LS, '.') end
  while string.find(LS.current, "%d") do
    self:save_and_next(LS)
  end
  if LS.current == "." then
    self:save_and_next(LS)
    if LS.current == "." then
      self:save_and_next(LS)
      self:lexerror(LS,
        "ambiguous syntax (decimal point x string concatenation)",
        "TK_NUMBER")
    end
  end
  while string.find(LS.current, "%d") do
    self:save_and_next(LS)
  end
  if LS.current == "e" or LS.current == "E" then
    self:save_and_next(LS)  -- read 'E'
    if LS.current == "+" or LS.current == "-" then
      self:save_and_next(LS)  -- optional exponent sign
    end
    while string.find(LS.current, "%d") do
      self:save_and_next(LS)
    end
  end
  local seminfo = tonumber(LS.buff)
  if not seminfo then
    self:lexerror(LS, "malformed number", "TK_NUMBER")
  end
  Token.seminfo = seminfo
end

------------------------------------------------------------------------
-- reads a long string or long comment
------------------------------------------------------------------------
function luaX:read_long_string(LS, Token)
  local cont = 0
  LS.buff = ""
  self:save(LS, "[")  -- save first '['
  self:save_and_next(LS)  -- pass the second '['
  if LS.current == "\n" then  -- string starts with a newline?
    self:inclinenumber(LS)  -- skip it
  end
  while true do
    local c = LS.current
    if c == "EOZ" then
      self:lexerror(LS, Token and "unfinished long string" or
                    "unfinished long comment", "TK_EOS")
    elseif c == "[" then
      self:save_and_next(LS)
      if LS.current == "[" then
        cont = cont + 1
        self:save_and_next(LS)
      end
    elseif c == "]" then
      self:save_and_next(LS)
      if LS.current == "]" then
        if cont == 0 then break end
        cont = cont - 1
        self:save_and_next(LS)
      end
    elseif c == "\n" then
      self:save(LS, "\n")
      self:inclinenumber(LS)
      if not Token then LS.buff = "" end -- reset buffer to avoid wasting space
    else
      self:save_and_next(LS)
    end--if c
  end--while
  self:save_and_next(LS)  -- skip the second ']'
  if Token then
    Token.seminfo = string.sub(LS.buff, 3, -3)
  end
end

------------------------------------------------------------------------
-- reads a string
------------------------------------------------------------------------
function luaX:read_string(LS, del, Token)
  LS.buff = ""
  self:save_and_next(LS)
  while LS.current ~= del do
    local c = LS.current
    if c == "EOZ" then
      self:lexerror(LS, "unfinished string", "TK_EOS")
    elseif c == "\n" then
      self:lexerror(LS, "unfinished string", "TK_STRING")
    elseif c == "\\" then
      c = self:next(LS)  -- do not save the '\'
      if c ~= "EOZ" then -- will raise an error next loop
        -- escapes handling greatly simplified here:
        local i = string.find("abfnrtv\n", c, 1, 1)
        if i then
          self:save(LS, string.sub("\a\b\f\n\r\t\v\n", i, i))
          if i == 8 then self:inclinenumber(LS) else self:next(LS) end
        elseif not string.find(c, "%d") then
          self:save_and_next(LS)  -- handles \\, \", \', and \?
        else  -- \xxx
          c, i = 0, 0
          repeat
            c = 10 * c + LS.current
            self:next(LS)
            i = i + 1
          until i >= 3 or not string.find(LS.current, "%d")
          if c > 255 then  -- UCHAR_MAX
            self:lexerror(LS, "escape sequence too large", "TK_STRING")
          end
          self:save(LS, string.char(c))
        end
      end
    else
      self:save_and_next(LS)
    end--if c
  end--while
  self:save_and_next(LS)  -- skip delimiter
  Token.seminfo = string.sub(LS.buff, 2, -2)
end

------------------------------------------------------------------------
-- main lexer function
------------------------------------------------------------------------
function luaX:lex(LS, Token)
  while true do
    local c = LS.current
    ----------------------------------------------------------------
    if c == "\n" then
      self:inclinenumber(LS)
    ----------------------------------------------------------------
    elseif c == "-" then
      c = self:next(LS)
      if c ~= "-" then return "-" end
      -- else is a comment
      c = self:next(LS)
      if c == "[" and self:next(LS) == "[" then
        self:read_long_string(LS)  -- long comment
      else  -- short comment
        c = LS.current
        while c ~= "\n" and c ~= "EOZ" do
          c = self:next(LS)
        end
      end
    ----------------------------------------------------------------
    elseif c == "[" then
      c = self:next(LS)
      if c ~= "[" then return "["
      else
        self:read_long_string(LS, Token)
        return "TK_STRING"
      end
    ----------------------------------------------------------------
    elseif c == "=" then
      c = self:next(LS)
      if c ~= "=" then return "="
      else self:next(LS); return "TK_EQ" end
    ----------------------------------------------------------------
    elseif c == "<" then
      c = self:next(LS)
      if c ~= "=" then return "<"
      else self:next(LS); return "TK_LE" end
    ----------------------------------------------------------------
    elseif c == ">" then
      c = self:next(LS)
      if c ~= "=" then return ">"
      else self:next(LS); return "TK_GE" end
    ----------------------------------------------------------------
    elseif c == "~" then
      c = self:next(LS)
      if c ~= "=" then return "~"
      else self:next(LS); return "TK_NE" end
    ----------------------------------------------------------------
    elseif c == "\"" or c == "'" then
      self:read_string(LS, c, Token)
      return "TK_STRING"
    ----------------------------------------------------------------
    elseif c == "." then
      c = self:next(LS)
      if c == "." then
        c = self:next(LS)
        if c == "." then
          self:next(LS)
          return "TK_DOTS"  -- ...
        else
          return "TK_CONCAT"  -- ..
        end
      elseif not string.find(c, "%d") then
        return '.'
      else
        self:read_numeral(LS, true, Token)
        return "TK_NUMBER"
      end
    ----------------------------------------------------------------
    elseif c == "EOZ" then
      return "TK_EOS"
    ----------------------------------------------------------------
    else  -- default
      if string.find(c, "%s") then
        self:next(LS)
      elseif string.find(c, "%d") then
        self:read_numeral(LS, false, Token)
        return "TK_NUMBER"
      elseif string.find(c, "[_%a]") then
        -- identifier or reserved word
        local l = self:readname(LS)
        local tok = self.string2token[l]
        if tok then return tok end  -- reserved word?
        Token.seminfo = l
        return "TK_NAME"
      else
        if string.find(c, "%c") then
          self:error(LS, "invalid control char",
                     string.format("char(%d)", string.byte(c)))
        end
        self:next(LS)
        return c  -- single-char tokens (+ - / ...)
      end
    ----------------------------------------------------------------
    end--if c
  end--while
end