summaryrefslogtreecommitdiff
path: root/compiler/ldscript.pas
blob: fd3d9e3a0c96e1c617951e1c7caf9083617bc1c6 (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
{
    Copyright (c) 2012 by Sergei Gorelkin

    A basic lexer for GNU ld scripts

    This program is free software; you can redistribute it and/or modify
    iu under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge- MA 02139, USA.

 ****************************************************************************
}

unit ldscript;

{$i fpcdefs.inc}

interface

uses
  owbase;

type
  TldScriptToken=char;

  TScriptLexer=class(TObject)
    data: ansistring;
    curtoken: TldScriptToken;
    curtokenstr: string;
    curpos: longint;
    line: longint;
    linestart: longint;
  public
    constructor Create(aReader:TObjectReader);
    procedure nextToken;
    function CheckForIdent(const s:string):boolean;
    function CheckFor(c:TldScriptToken):boolean;
    procedure Expect(c:TldScriptToken);
    property token:TldScriptToken read curtoken;
    property tokenstr:string read curtokenstr;
  end;

const
  tkEOF      = #0;
  tkINVALID  = #1;
  tkIDENT    = #2;
  tkNUMBER   = #3;
  tkLITERAL  = #4;

  tkLSHIFT   = #5;   { << }
  tkLE       = #6;   { <= }
  tkRSHIFT   = #7;   { >> }
  tkGE       = #8;   { >= }
  tkANDAND   = #9;   { && }
  tkANDEQ    = #10;   { &= }
  tkOROR     = #11;   { || }
  tkOREQ     = #12;   { |= }
  tkDIVEQ    = #13;   { /= }
  tkMULTEQ   = #14;   { *= }
  tkMINUSEQ  = #15;   { -= }
  tkPLUSEQ   = #16;   { += }
  tkNE       = #17;   { != }
  tkEQ       = #18;   { == }

  tkRSHIFTEQ = #19;   { >>= }
  tkLSHIFTEQ = #20;   { <<= }

implementation

uses
  sysutils;

const
  NameChars=['A'..'Z','a'..'z','_','.','$','0'..'9','+','-','=',',','*','?','/','~','\','[',']'];


{*****************************************************************************
                               TSCRIPTLEXER
*****************************************************************************}

constructor TScriptLexer.Create(AReader:TObjectReader);
begin
  { Expected data size is few hundred bytes,  }
  SetLength(data,AReader.size);
  AReader.Read(data[1],AReader.size);
  curpos:=1;
end;

procedure TScriptLexer.nextToken;
  var
    p,start: longint;
  begin
    p:=curpos;
    repeat
      { skip whitespace }
      while (data[p] in [#32,#9,#13]) do
        inc(p);
      start:=p;
      { C-style comment }
      if (data[p]='/') and (data[p+1]='*') then
        begin
          inc(p,2);
          while (data[p]<>'*') and (data[p+1]<>'/') do
            begin
              if (data[p]=#0) then
                begin
                  curtoken:=tkINVALID;
                  exit;
                end;
              if (data[p]=#10) then
                begin
                  inc(line);
                  linestart:=p+1;
                end;
              inc(p);
            end;
          inc(p,2);
          continue;
        end
      else if (data[p]=#10) then
        begin
          inc(p);
          inc(line);
          linestart:=p;
          continue;
        end
      else if (data[p]='#') then  { line comment }
        begin
          inc(p);
          while (data[p]<>#0) and (data[p]<>#10) do
            inc(p);
          continue;
        end;

      case data[p] of
        #0: curtoken:=tkEOF;

        '/':
          if (data[p+1] in NameChars) then
            begin
              inc(p);
              while (data[p] in NameChars) do
                inc(p);
              curtoken:=tkIDENT;
            end
          else if (data[p+1]='=') then
            curtoken:=tkDIVEQ
          else
            curtoken:='/';

        'A'..'Z','a'..'z','_','.','$','\':
          begin
            inc(p);
            while (data[p] in NameChars) do
              inc(p);
            curtoken:=tkIDENT;
          end;

        '0'..'9':
          begin
            if (data[p]='0') and (data[p+1] in ['x','X']) then
              begin
                inc(p,2);
                while data[p] in ['0'..'9','a'..'f','A'..'F'] do
                  inc(p);
              end
            else
              while (data[p] in ['0'..'9']) do
                inc(p);
            curtoken:=tkNUMBER;
          end;

        '"':
          begin
            inc(p);
            while (data[p]<>'"') and (data[p]<>#10) do
              inc(p);
            if data[p]=#10 then
              begin
                curtoken:=tkINVALID;
                exit;
              end;
            inc(p);
            curtoken:=tkLITERAL;
          end;

        '<':
          if (data[p+1]='<') then
            begin
              if (data[p+2]='=') then
                curtoken:=tkLSHIFTEQ
              else
                curtoken:=tkLSHIFT;
            end
          else if (data[p+1]='=') then
            curtoken:=tkLE
          else
            curtoken:='<';

        '>':
          if (data[p+1]='>') then
            begin
              if (data[p+2]='=') then
                curtoken:=tkRSHIFTEQ
              else
                curtoken:=tkRSHIFT;
            end
          else if (data[p+1]='=') then
            curtoken:=tkGE
          else
            curtoken:='>';

        '!':
          if (data[p+1]='=') then
            curtoken:=tkNE
          else
            curtoken:='!';

        '&':
          if (data[p+1]='&') then
            curtoken:=tkANDAND
          else if (data[p+1]='=') then
            curtoken:=tkANDEQ
          else
            curtoken:='&';

        '|':
          if (data[p+1]='|') then
            curtoken:=tkOROR
          else if (data[p+1]='=') then
            curtoken:=tkOREQ
          else
            curtoken:='|';

        '*':
          if (data[p+1]='=') then
            curtoken:=tkMULTEQ
          else
            curtoken:='*';

        '+':
          if (data[p+1]='=') then
            curtoken:=tkPLUSEQ
          else
            curtoken:='+';

        '-':
          if (data[p+1]='=') then
            curtoken:=tkMINUSEQ
          else
            curtoken:='-';

        '=':
          if (data[p+1]='=') then
            curtoken:=tkEQ
          else
            curtoken:='=';

        '(',')','{','}','[',']',';','?',':':
          curtoken:=data[p];
      else
        curtoken:=tkINVALID;
        exit;
      end;
      break;
    until false;
    case curtoken of
      tkRSHIFTEQ,tkLSHIFTEQ: inc(p,3);
      tkLSHIFT..tkEQ: inc(p,2);
      #32..#255: inc(p);
      tkIDENT,tkNUMBER:
        setstring(curtokenstr,@data[start],p-start);
      tkLITERAL:
        setstring(curtokenstr,@data[start+1],p-start-2);
    end;
    curpos:=p;
  end;

procedure TScriptLexer.Expect(c:TldScriptToken);
  begin
    if (curtoken=c) then
      nextToken
    else
      {error};
  end;

function TScriptLexer.CheckForIdent(const s:string):boolean;
  begin
    result:=(curtoken=tkIDENT) and (curtokenstr=s);
    if result then
      nextToken;
  end;

function TScriptLexer.CheckFor(c:TldScriptToken):boolean;
  begin
    result:=(curtoken=c);
    if result then
      nextToken;
  end;

end.