-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreparegrammar.pp
More file actions
120 lines (110 loc) · 2.25 KB
/
preparegrammar.pp
File metadata and controls
120 lines (110 loc) · 2.25 KB
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
program preparegrammar;
{$mode objfpc}
{$h+}
uses sysutils;
type
Tstate = (comment, other,ident,quoted);
function QuoteIdents(var state: tstate; aLine : string) : string;
var
I : integer;
c : char;
whitespaceonly : boolean;
isWhitespace : boolean;
function NextChar : Char;
begin
if I<Length(aLine) then
Result:=aLine[i+1]
else
Result:=#0;
end;
begin
whitespaceonly:=true;
result:='';
I:=1;
While I<=Length(aLine) do
begin
C:=aLine[i];
isWhiteSpace:=false;
case c of
'a'..'z',
'_',
'A'..'Z' :
begin
if state=other then
begin
result:=Result+'<';
state:=ident;
end;
if c='_' then
Result:=Result+'\';
end;
'"' :
if state=quoted then
state:=other
else
state:=quoted;
'(' : if (state<>comment) and (nextchar='*') then
begin
state:=comment;
inc(i)
end;
'*' : if (state=Comment) and (nextchar=')') then
begin
inc(i);
c:=' ';
state:=other;
end;
else
if state=ident then
begin
Result:=Result+'>';
state:=other;
end;
case C of
' ' : iswhitespace:=True;
'|' : if whitespaceonly then
begin
result:=Result+'\al';
c:='t';
end;
'&' : Result:=Result+'\';
end;
end;
inc(i);
if not (state=comment) then
Result:=Result+c;
if whitespaceonly and not isWhiteSpace then
whitespaceonly := False;
end;
if state=ident then
begin
Result:=Result+'>';
state:=other
end;
end;
var
prev_empty : boolean;
state : tstate;
Line, quotedLine: String;
begin
prev_empty:=false;
state:=other;
While not EOF do
begin
Readln(Line);
QuotedLine:=QuoteIdents(state,Line);
if Trim(QuotedLine)<>'' then
begin
Writeln(QuotedLine);
prev_empty:=false;
end
else
begin
if not prev_empty then
Writeln('');
prev_empty:=True;
end;
if not (state=comment) then
state:=other;
end;
end.