aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/lzio_mk4.lua
blob: d97c79566bf184392246d26be38fba113b0a04d6 (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
--[[--------------------------------------------------------------------

  lzio.lua
  Lua 5 buffered streams in Lua
  This file is part of Yueliang.

  Copyright (c) 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:
-- * this is a line-based input streamer for the MK4 lexer
-- * all EOL (end-of-line) characters are translated to "\n"
-- * if last line in a file does not have an EOL character, this
--   streamer adds one, the ambiguity is due to "*l" stripping
-- * EOF uses an empty string to simplify testing in lexer
----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- local zio_init = require("lzio.lua")
-- local z = zio_init("@<filename>")
-- local z = zio_init("<string>")
-- z:getln()
-- * get next line from input stream
-- z.name
-- * name of the chunk, "@<filename>" or "=string"
----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- Format of z structure (ZIO)
-- z.getln   -- chunk reader function, reads line-by-line
-- z.name    -- name of stream
----------------------------------------------------------------------]]

return
function(buff)
--[[--------------------------------------------------------------------
-- initialize reader
-- * reader should return a string with an EOL character, or an empty
--   string if there is nothing else to parse
----------------------------------------------------------------------]]
  local reader
  local z = {}
  if string.sub(buff, 1, 1) == "@" then
    ----------------------------------------------------------------
    -- create a chunk reader function from a source file
    ----------------------------------------------------------------
    z.name = buff
    local h = io.open(string.sub(buff, 2), "r")
    if not h then return nil end
    reader = function()
      if not h or io.type(h) == "closed file" then return nil end
      local data = h:read("*l")
      if not data then h:close(); return "" end
      return data.."\n"
    end
  else
    ----------------------------------------------------------------
    -- create a chunk reader function from a source string
    ----------------------------------------------------------------
    z.name = "=string"
    reader = function()
      if not buff then return nil end
      local p, q, data, eol = string.find(buff, "([^\r\n]*)(\r?\n?)")
      buff = string.sub(buff, q + 1)
      if data == "" and eol == "" then return "" end
      return data..eol
    end
  end
--[[--------------------------------------------------------------------
-- initialize input stream object
----------------------------------------------------------------------]]
  if not reader then return end
  z.getln = reader
  return z
--[[------------------------------------------------------------------]]
end