~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/synedit/synhighlighterpo.pp

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-------------------------------------------------------------------------------
 
2
The contents of this file may be used under the terms of the
 
3
GNU General Public License Version 2 or later (the "GPL")
 
4
 
 
5
Software distributed under the License is distributed on an "AS IS" basis,
 
6
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 
7
the specific language governing rights and limitations under the License.
 
8
 
 
9
The Original Code is: SynHighlighterPo.pp, released 2011-12-17.
 
10
Author: Bart Broersma
 
11
All Rights Reserved.
 
12
 
 
13
Contributors to the SynEdit and mwEdit projects are listed in the
 
14
Contributors.txt file.
 
15
 
 
16
 
 
17
$Id: SynHighlighterPo.pp,v 0.0.1 bbroersma Exp $
 
18
 
 
19
 
 
20
Known Issues:
 
21
-------------------------------------------------------------------------------}
 
22
{
 
23
@abstract(Provides a po-files highlighter for SynEdit)
 
24
@author(Bart Broersma)
 
25
@created(2011-12-17)
 
26
@lastmod(2011-12-18)
 
27
The SynHighlighterPo unit provides SynEdit with an po-files highlighter.
 
28
}
 
29
unit SynHighlighterPo;
 
30
 
 
31
{$I SynEdit.inc}
 
32
 
 
33
interface
 
34
 
 
35
uses
 
36
  Classes, SysUtils,
 
37
  {$IFDEF SYN_CLX}
 
38
  QGraphics,
 
39
  {$ELSE}
 
40
  Graphics,
 
41
  {$ENDIF}
 
42
  SynEditTypes, SynEditHighlighter, SynEditStrConst;
 
43
 
 
44
type
 
45
  TtkTokenKind = (tkComment, tkText, tkKey, tkNull, tkSpace, tkString,
 
46
                  tkIdentifier, tkPrevValue, tkFlags, tkUnknown);
 
47
 
 
48
  TProcTableProc = procedure of object;
 
49
 
 
50
type
 
51
 
 
52
  { TSynPoSyn }
 
53
 
 
54
  TSynPoSyn = class(TSynCustomHighlighter)
 
55
  private
 
56
    fLine: PChar;
 
57
    fLineNumber: Integer;
 
58
    fProcTable: array[#0..#255] of TProcTableProc;
 
59
    Run: LongInt;
 
60
    fTokenPos: Integer;
 
61
    FTokenID: TtkTokenKind;
 
62
    fCommentAttri: TSynHighlighterAttributes;
 
63
    fTextAttri: TSynHighlighterAttributes;
 
64
    fKeyAttri: TSynHighlighterAttributes;
 
65
    fSpaceAttri: TSynHighlighterAttributes;
 
66
    fStringAttri: TSynHighlighterAttributes;
 
67
    fIdentAttri: TSynHighlighterAttributes;
 
68
    fPrevAttri: TSynHighlighterAttributes;
 
69
    fFlagAttri: TSynHighlighterAttributes;
 
70
    procedure IdentProc;
 
71
    procedure KeyProc;
 
72
    procedure CRProc;
 
73
    procedure TextProc;
 
74
    procedure LFProc;
 
75
    procedure NullProc;
 
76
    procedure HashProc;
 
77
    procedure SpaceProc;
 
78
    procedure StringProc;
 
79
    procedure MakeMethodTables;
 
80
  protected
 
81
    {General Stuff}
 
82
    function GetIdentChars: TSynIdentChars; override;
 
83
    function GetSampleSource: String; override;
 
84
  public
 
85
    class function GetLanguageName: string; override;
 
86
    function IsKeyword(const AKeyword: string): boolean; override;
 
87
  public
 
88
    constructor Create(AOwner: TComponent); override;
 
89
    function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
 
90
      override;
 
91
    function GetEol: Boolean; override;
 
92
    function GetTokenID: TtkTokenKind;
 
93
    procedure SetLine(const NewValue: String; LineNumber:Integer); override;
 
94
    function GetToken: String; override;
 
95
    procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); override;
 
96
    function GetTokenAttribute: TSynHighlighterAttributes; override;
 
97
    function GetTokenKind: integer; override;
 
98
    function GetTokenPos: Integer; override;
 
99
    procedure Next; override;
 
100
  published
 
101
    property CommentAttri: TSynHighlighterAttributes read fCommentAttri
 
102
      write fCommentAttri;
 
103
    property TextAttri   : TSynHighlighterAttributes read fTextAttri
 
104
      write fTextAttri;
 
105
    property KeyAttri    : TSynHighlighterAttributes read fKeyAttri
 
106
      write fKeyAttri;
 
107
    property SpaceAttri  : TSynHighlighterAttributes read fSpaceAttri
 
108
      write fSpaceAttri;
 
109
  end;
 
110
 
 
111
implementation
 
112
 
 
113
 
 
114
 
 
115
const
 
116
  PoKeysCount = 3;
 
117
  PoKeys: array[1..PoKeysCount] of string = (
 
118
    'msgid', 'msgstr', 'msgctxt');
 
119
 
 
120
 
 
121
procedure TSynPoSyn.MakeMethodTables;
 
122
var
 
123
  i: Char;
 
124
begin
 
125
  for i := #0 to #255 do
 
126
    case i of
 
127
      #0      : fProcTable[i] := @NullProc;
 
128
      #10 {LF}: fProcTable[i] := @LFProc;
 
129
      #13 {CR}: fProcTable[i] := @CRProc;
 
130
      #34 {"} : fProcTable[i] := @StringProc;
 
131
      'A'..'Z', 'a'..'z', '_': fProcTable[I] := @IdentProc;
 
132
      '#' {#} : fProcTable[i] := @HashProc;
 
133
      #1..#9, #11, #12, #14..#32: fProcTable[i] := @SpaceProc;
 
134
    else
 
135
      fProcTable[i] := @TextProc;
 
136
    end;
 
137
end;
 
138
 
 
139
constructor TSynPoSyn.Create(AOwner: TComponent);
 
140
begin
 
141
  inherited Create(AOwner);
 
142
  fCommentAttri            := TSynHighlighterAttributes.Create(SYNS_AttrComment);
 
143
  fCommentAttri.Style      := [fsItalic];
 
144
  fCommentAttri.Foreground := clGreen;
 
145
  AddAttribute(fCommentAttri);
 
146
 
 
147
  fTextAttri               := TSynHighlighterAttributes.Create(SYNS_AttrText);
 
148
  AddAttribute(fTextAttri);
 
149
 
 
150
  fKeyAttri                := TSynHighlighterAttributes.Create(SYNS_AttrKey);
 
151
  fKeyAttri.Foreground     := clBlue;
 
152
  fKeyAttri.Style          := [fsBold];
 
153
  AddAttribute(fKeyAttri);
 
154
 
 
155
  fIdentAttri := TSynHighlighterAttributes.Create(SYNS_AttrIdentifier, SYNS_XML_AttrIdentifier);
 
156
  fIdentAttri.Foreground   := clGreen;
 
157
  fIdentAttri.Style        := [fsBold];
 
158
  AddAttribute(fIdentAttri);
 
159
 
 
160
  fPrevAttri  := TSynHighlighterAttributes.Create(SYNS_AttrPrevValue, SYNS_XML_AttrPrevValue);
 
161
  fPrevAttri.Foreground    := clOlive;
 
162
  fPrevAttri.Style         := [fsItalic];
 
163
  AddAttribute(fPrevAttri);
 
164
 
 
165
  fFlagAttri  := TSynHighlighterAttributes.Create(SYNS_AttrFlags, SYNS_XML_AttrFlags);
 
166
  fFlagAttri.Foreground    := clTeal;
 
167
  AddAttribute(fFlagAttri);
 
168
 
 
169
  fSpaceAttri              := TSynHighlighterAttributes.Create(SYNS_AttrSpace, SYNS_XML_AttrSpace);
 
170
  AddAttribute(fSpaceAttri);
 
171
 
 
172
  fStringAttri             := TSynHighlighterAttributes.Create(SYNS_AttrString, SYNS_XML_AttrString);
 
173
  fStringAttri.Foreground  := clFuchsia;
 
174
  AddAttribute(fStringAttri);
 
175
 
 
176
  SetAttributesOnChange(@DefHighlightChange);
 
177
 
 
178
  fDefaultFilter      := SYNS_FilterPo;
 
179
  MakeMethodTables;
 
180
end; { Create }
 
181
 
 
182
procedure TSynPoSyn.SetLine(const NewValue: String; LineNumber:Integer);
 
183
begin
 
184
  inherited;
 
185
  fLine := PChar(NewValue);
 
186
  Run := 0;
 
187
  fLineNumber := LineNumber;
 
188
  Next;
 
189
end;
 
190
 
 
191
 
 
192
procedure TSynPoSyn.IdentProc;
 
193
begin
 
194
  while fLine[Run] in GetIdentChars {['A'..'Z','a'..'z']} do inc(Run);
 
195
  if IsKeyWord(GetToken) then begin
 
196
    fTokenId := tkKey;
 
197
    Exit;
 
198
  end
 
199
  else fTokenId := tkUnknown;
 
200
end;
 
201
 
 
202
procedure TSynPoSyn.CRProc;
 
203
begin
 
204
  fTokenID := tkSpace;
 
205
  Case FLine[Run + 1] of
 
206
    #10: inc(Run, 2);
 
207
  else inc(Run);
 
208
  end;
 
209
end;
 
210
 
 
211
 
 
212
procedure TSynPoSyn.KeyProc;
 
213
begin
 
214
  fTokenID := tkKey;
 
215
  inc(Run);
 
216
  while FLine[Run] <> #0 do
 
217
    case FLine[Run] of
 
218
      #32: break;
 
219
      #10: break;
 
220
      #13: break;
 
221
    else inc(Run);
 
222
    end;
 
223
end;
 
224
 
 
225
procedure TSynPoSyn.TextProc;
 
226
begin
 
227
  if Run = 0 then
 
228
    IdentProc
 
229
  else begin
 
230
    inc(Run);
 
231
    {$IFDEF SYN_LAZARUS}
 
232
    while (fLine[Run] in [#128..#191]) OR // continued utf8 subcode
 
233
     ((fLine[Run]<>#0) and (fProcTable[fLine[Run]] = @TextProc)) do inc(Run);
 
234
    {$ENDIF}
 
235
    fTokenID := tkText;
 
236
  end;
 
237
end;
 
238
 
 
239
procedure TSynPoSyn.LFProc;
 
240
begin
 
241
  fTokenID := tkSpace;
 
242
  inc(Run);
 
243
end;
 
244
 
 
245
procedure TSynPoSyn.NullProc;
 
246
begin
 
247
  fTokenID := tkNull;
 
248
end;
 
249
 
 
250
 
 
251
 
 
252
procedure TSynPoSyn.SpaceProc;
 
253
begin
 
254
  inc(Run);
 
255
  fTokenID := tkSpace;
 
256
  while FLine[Run] in [#1..#9, #11, #12, #14..#32] do inc(Run);
 
257
end;
 
258
 
 
259
 
 
260
procedure TSynPoSyn.StringProc;
 
261
begin
 
262
  fTokenID := tkString;
 
263
  repeat
 
264
    case FLine[Run] of
 
265
      #0, #10, #13: break;
 
266
      '\': if FLine[Run + 1] = #34 then Inc(Run); { \" means a literal " in the line}
 
267
    end;
 
268
    inc(Run);
 
269
  until FLine[Run] = #34;
 
270
  if FLine[Run] <> #0 then inc(Run);
 
271
end;
 
272
 
 
273
 
 
274
procedure TSynPoSyn.HashProc;
 
275
begin
 
276
  // if it is not column 0 mark as tkText and get out of here
 
277
  if Run > 0 then
 
278
  begin
 
279
    fTokenID := tkText;
 
280
    inc(Run);
 
281
    Exit;
 
282
  end;
 
283
 
 
284
  // this is column 0 --> ok
 
285
  fTokenID := tkComment;
 
286
 
 
287
  while FLine[Run] <> #0 do
 
288
    case FLine[Run] of
 
289
      #10: break;
 
290
      #13: break;
 
291
      ':': begin if (Run = 1) then fTokenId := tkIdentifier; Inc(Run) end;
 
292
      ',': begin if (Run = 1) then  fTokenId := tkFlags;  Inc(Run) end;
 
293
      '|': begin if (Run = 1) then  fTokenId := tkPrevValue; Inc(Run) end;
 
294
    else inc(Run);
 
295
    end;
 
296
end;
 
297
 
 
298
procedure TSynPoSyn.Next;
 
299
begin
 
300
  fTokenPos := Run;
 
301
  fProcTable[fLine[Run]];
 
302
end;
 
303
 
 
304
function TSynPoSyn.GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
 
305
begin
 
306
  case Index of
 
307
    SYN_ATTR_COMMENT: Result := fCommentAttri;
 
308
    SYN_ATTR_KEYWORD: Result := fKeyAttri;
 
309
    SYN_ATTR_STRING: Result := fStringAttri;
 
310
    SYN_ATTR_WHITESPACE: Result := fSpaceAttri;
 
311
  else
 
312
    Result := nil;
 
313
  end;
 
314
end;
 
315
 
 
316
function TSynPoSyn.GetEol: Boolean;
 
317
begin
 
318
  Result := fTokenId = tkNull;
 
319
end;
 
320
 
 
321
function TSynPoSyn.GetToken: String;
 
322
var
 
323
  Len: LongInt;
 
324
begin
 
325
  Len := Run - fTokenPos;
 
326
  SetString(Result, (FLine + fTokenPos), Len);
 
327
end;
 
328
 
 
329
procedure TSynPoSyn.GetTokenEx(out TokenStart: PChar; out TokenLength: integer);
 
330
begin
 
331
  TokenLength := Run - fTokenPos;
 
332
  TokenStart := FLine + fTokenPos;
 
333
end;
 
334
 
 
335
function TSynPoSyn.GetTokenID: TtkTokenKind;
 
336
begin
 
337
  Result := fTokenId;
 
338
end;
 
339
 
 
340
function TSynPoSyn.GetTokenAttribute: TSynHighlighterAttributes;
 
341
begin
 
342
  case fTokenID of
 
343
    tkComment: Result := fCommentAttri;
 
344
    tkText   : Result := fTextAttri;
 
345
    tkKey    : Result := fKeyAttri;
 
346
    tkSpace  : Result := fSpaceAttri;
 
347
    tkString : Result := fStringAttri;
 
348
    tkIdentifier: Result := fIdentAttri;
 
349
    tkFlags:       Result := fFlagAttri;
 
350
    tkPrevValue:  Result := fPrevAttri;
 
351
    tkUnknown: Result := fTextAttri;
 
352
    else Result := nil;
 
353
  end;
 
354
end;
 
355
 
 
356
function TSynPoSyn.GetTokenKind: integer;
 
357
begin
 
358
  Result := Ord(fTokenId);
 
359
end;
 
360
 
 
361
function TSynPoSyn.GetTokenPos: Integer;
 
362
begin
 
363
 Result := fTokenPos;
 
364
end;
 
365
 
 
366
function TSynPoSyn.GetIdentChars: TSynIdentChars;
 
367
begin
 
368
  Result := inherited GetIdentChars; //TSynValidStringChars;
 
369
end;
 
370
 
 
371
class function TSynPoSyn.GetLanguageName: string;
 
372
begin
 
373
  Result := SYNS_LangPo;
 
374
end;
 
375
 
 
376
function TSynPoSyn.GetSampleSource: String;
 
377
begin
 
378
  Result := '"Project-Id-Version: \n"' + LineEnding +
 
379
            '"POT-Creation-Date: \n"' + LineEnding +
 
380
            '"MIME-Version: 1.0\n"' + LineEnding +
 
381
            '"Content-Type: text/plain; charset=UTF-8\n"' + LineEnding +
 
382
            '"Content-Transfer-Encoding: 8bit\n"' + LineEnding +
 
383
            LineEnding +
 
384
            '#: lazarusidestrconsts.dlgcochecks' + LineEnding +
 
385
            '#, fuzzy' + LineEnding +
 
386
            '#| msgid "Checks:"' + LineEnding +
 
387
            'msgid "Checks"' + LineEnding +
 
388
            'msgstr "Controleert:"' + LineEnding +
 
389
            LineEnding +
 
390
            '#: lazarusidestrconsts.listemplateeditparamcellhelp' + LineEnding +
 
391
            'msgid ""' + LineEnding +
 
392
            '"Inserts an editable Cell, with a default value\n"' + LineEnding +
 
393
            '"\"\",Sync=n (,S=n), to Sync with a previous cell (n=1 to highest prev cell\n"' + LineEnding +
 
394
            '"\"default\",Sync, to Sync with a previous cell of equal default\n"' + LineEnding +
 
395
            'msgstr ""';
 
396
 
 
397
end;
 
398
 
 
399
function TSynPoSyn.IsKeyword(const AKeyword: string): boolean;
 
400
var
 
401
  Token: String;
 
402
  i: Integer;
 
403
begin
 
404
  //There are only 3 keywords, so no need to make a hashtable
 
405
  Token := LowerCase(AKeyWord);
 
406
  for i := 1 to PoKeysCount do if (PoKeys[i] = Token) then
 
407
  begin
 
408
    Exit(True);
 
409
  end;
 
410
  Result := False;
 
411
end;
 
412
 
 
413
initialization
 
414
  RegisterPlaceableHighlighter(TSynPoSyn);
 
415
 
 
416
end.