summary refs log tree commit diff stats
path: root/compiler/nimlexbase.nim
blob: 214147a2b197b4e2bea861c5e34ce450a37d746a (plain) (blame)
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
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# Base Object of a lexer with efficient buffer handling. In fact
# I believe that this is the most efficient method of buffer
# handling that exists! Only at line endings checks are necessary
# if the buffer needs refilling.

import
  llstream, strutils

const
  Lrz* = ' '
  Apo* = '\''
  Tabulator* = '\x09'
  ESC* = '\x1B'
  CR* = '\x0D'
  FF* = '\x0C'
  LF* = '\x0A'
  BEL* = '\x07'
  BACKSPACE* = '\x08'
  VT* = '\x0B'

const
  EndOfFile* = '\0'           # end of file marker
                              # A little picture makes everything clear :-)
                              #  buf:
                              #  "Example Text\n ha!"   bufLen = 17
                              #   ^pos = 0     ^ sentinel = 12
                              #
  NewLines* = {CR, LF}

type
  TBaseLexer* = object of RootObj
    bufpos*: int
    buf*: string
    stream*: PLLStream        # we read from this stream
    lineNumber*: int          # the current line number
                              # private data:
    sentinel*: int
    lineStart*: int           # index of last line start in buffer
    offsetBase*: int          # use ``offsetBase + bufpos`` to get the offset


proc openBaseLexer*(L: var TBaseLexer, inputstream: PLLStream,
                    bufLen: int = 8192)
  # 8K is a reasonable buffer size
proc closeBaseLexer*(L: var TBaseLexer)
proc getCurrentLine*(L: TBaseLexer, marker: bool = true): string
proc getColNumber*(L: TBaseLexer, pos: int): int
proc handleCR*(L: var TBaseLexer, pos: int): int
  # Call this if you scanned over CR in the buffer; it returns the
  # position to continue the scanning from. `pos` must be the position
  # of the CR.
proc handleLF*(L: var TBaseLexer, pos: int): int
  # Call this if you scanned over LF in the buffer; it returns the the
  # position to continue the scanning from. `pos` must be the position
  # of the LF.
# implementation

proc closeBaseLexer(L: var TBaseLexer) =
  llStreamClose(L.stream)

proc fillBuffer(L: var TBaseLexer) =
  var
    charsRead, toCopy, s: int # all are in characters,
                              # not bytes (in case this
                              # is not the same)
    oldBufLen: int
  # we know here that pos == L.sentinel, but not if this proc
  # is called the first time by initBaseLexer()
  assert(L.sentinel < L.buf.len)
  toCopy = L.buf.len - L.sentinel - 1
  assert(toCopy >= 0)
  if toCopy > 0:
    moveMem(addr L.buf[0], addr L.buf[L.sentinel + 1], toCopy)
    # "moveMem" handles overlapping regions
  charsRead = llStreamRead(L.stream, addr L.buf[toCopy], L.sentinel + 1)
  s = toCopy + charsRead
  if charsRead < L.sentinel + 1:
    L.buf[s] = EndOfFile      # set end marker
    L.sentinel = s
  else:
    # compute sentinel:
    dec(s)                    # BUGFIX (valgrind)
    while true:
      assert(s < L.buf.len)
      while (s >= 0) and not (L.buf[s] in NewLines): dec(s)
      if s >= 0:
        # we found an appropriate character for a sentinel:
        L.sentinel = s
        break
      else:
        # rather than to give up here because the line is too long,
        # double the buffer's size and try again:
        oldBufLen = L.buf.len
        L.buf.setLen(L.buf.len * 2)
        assert(L.buf.len - oldBufLen == oldBufLen)
        charsRead = llStreamRead(L.stream, addr(L.buf[oldBufLen]),
                                 oldBufLen)
        if charsRead < oldBufLen:
          L.buf[oldBufLen + charsRead] = EndOfFile
          L.sentinel = oldBufLen + charsRead
          break
        s = L.buf.len - 1

proc fillBaseLexer(L: var TBaseLexer, pos: int): int =
  assert(pos <= L.sentinel)
  if pos < L.sentinel:
    result = pos + 1          # nothing to do
  else:
    fillBuffer(L)
    L.offsetBase += pos + 1
    L.bufpos = 0
    result = 0
  L.lineStart = result

proc handleCR(L: var TBaseLexer, pos: int): int =
  assert(L.buf[pos] == CR)
  inc(L.lineNumber)
  result = fillBaseLexer(L, pos)
  if L.buf[result] == LF:
    result = fillBaseLexer(L, result)

proc handleLF(L: var TBaseLexer, pos: int): int =
  assert(L.buf[pos] == LF)
  inc(L.lineNumber)
  result = fillBaseLexer(L, pos) #L.lastNL := result-1; // BUGFIX: was: result;

proc skipUTF8BOM(L: var TBaseLexer) =
  if L.buf[0] == '\xEF' and L.buf[1] == '\xBB' and L.buf[2] == '\xBF':
    inc(L.bufpos, 3)
    inc(L.lineStart, 3)

proc openBaseLexer(L: var TBaseLexer, inputstream: PLLStream, bufLen = 8192) =
  assert(bufLen > 0)
  L.bufpos = 0
  L.offsetBase = 0
  L.buf = newString(bufLen)
  L.sentinel = bufLen - 1
  L.lineStart = 0
  L.lineNumber = 1            # lines start at 1
  L.stream = inputstream
  fillBuffer(L)
  skipUTF8BOM(L)

proc getColNumber(L: TBaseLexer, pos: int): int =
  result = abs(pos - L.lineStart)

proc getCurrentLine(L: TBaseLexer, marker: bool = true): string =
  result = ""
  var i = L.lineStart
  while not (L.buf[i] in {CR, LF, EndOfFile}):
    add(result, L.buf[i])
    inc(i)
  result.add("\n")
  if marker:
    result.add(spaces(getColNumber(L, L.bufpos)) & '^' & "\n")
rvers that can act as firewall gateways and caching servers. They are preferable to the older gateway servers. Each protocol used by Lynx can be mapped separately using PROTOCOL_proxy environment variables of the form: UNIX setenv http_proxy "http://some.server.dom:port/" setenv https_proxy "http://some.server.dom:port/" setenv ftp_proxy "http://some.server.dom:port/" setenv gopher_proxy "http://some.server.dom:port/" setenv news_proxy "http://some.server.dom:port/" setenv newspost_proxy "http://some.server.dom:port/" setenv newsreply_proxy "http://some.server.dom:port/" setenv snews_proxy "http://some.server.dom:port/" setenv snewspost_proxy "http://some.server.dom:port/" setenv snewsreply_proxy "http://some.server.dom:port/" setenv nntp_proxy "http://some.server.dom:port/" setenv wais_proxy "http://some.server.dom:port/" setenv finger_proxy "http://some.server.dom:port/" setenv cso_proxy "http://some.server.dom:port/" VMS define "http_proxy" "http://some.server.dom:port/" define "https_proxy" "http://some.server.dom:port/" define "ftp_proxy" "http://some.server.dom:port/" define "gopher_proxy" "http://some.server.dom:port/" define "news_proxy" "http://some.server.dom:port/" define "newspost_proxy" "http://some.server.dom:port/" define "newsreply_proxy" "http://some.server.dom:port/" define "snews_proxy" "http://some.server.dom:port/" define "snewspost_proxy" "http://some.server.dom:port/" define "snewsreply_proxy" "http://some.server.dom:port/" define "nntp_proxy" "http://some.server.dom:port/" define "wais_proxy" "http://some.server.dom:port/" define "finger_proxy" "http://some.server.dom:port/" define "cso_proxy" "http://some.server.dom:port/" (Encase *BOTH* strings in double-quotes to maintain lower case for the PROTOCOL_proxy variable and for the http access type; include /system if you want proxying for all clients on your system.) If you wish to override the use of a proxy server for specific hosts or entire domains you may use the "no_proxy" environment variable. Here is an example use of "no_proxy": UNIX setenv no_proxy "host.domain.dom, domain1.dom, domain2" VMS define "no_proxy" "host.domain.dom, domain1.dom, domain2" You can include a port number in the no_proxy list to override use of a proxy server for the host accessed via that port, but not via other ports. For example, if you use "host.domain.dom:119" and/or "host.domain.dom:210", then news (port 119) URLs and/or any wais (port 210) searches on that host would be excluded, but http, ftp, and gopher services (if normally proxied) would still be included, as would any news or wais services on other hosts. If you wish to override the use of a proxy server completely (i.e., globally override any existing proxy variables), set the value of "no_proxy" to "*". Note that Lynx treats file URLs on the local host as requests for direct access to the file, and does not attempt ftp if that fails. It treats both ftp URLs and file URLs on remote hosts as ftp URLs, and does not attempt direct file access for either. If ftp URLs are being proxied, file URLs on a remote host will be converted to ftp URLs before submission by Lynx to the proxy server, so no special procedure for inducing the proxy server to handle them is required. Other WWW clients may require that the http server's configuration file have "Map file:* ftp:*" in it to perform that conversion. The proxy and no_proxy variables also can be set at run time via lynx.cfg. Copies of the Lynx online help are included in the lynx_help subdirectory tree and should be made accessible in response to the Lynx 'h'elp command by defining HELPFILE in userdefs.h and/or lynx.cfg to an appropriate file://localhost/path URL. UNIX & VMS Step 6. (Hopefully Optional) If something doesn't work, or you can't get it to compile at all, or you can't figure out what one of the defines means, or if you just want to make a comment, send an email message to the Lynx-Dev mailing list (see the README file about subscribing to Lynx-Dev). Until Lynx has been ported to all the world's operating systems, we expect there will be some compatibility problems, but we'll do our best to help you.