Debian Codegraph @

ipl/packs/euler/eulerscn.icn

raw
1
2 global inputFile
3 global inputLine,inputLineNumber,inputColumn,eoiToken
4 global keywordSet
5
6 procedure initScanner(filename)
7 inputFile := open(filename,"r") |
8 stop("unable to open input: ",filename)
9 return
10 end
11
12 procedure fractionPart()
13 return ="." || (tab(many(&digits)) | "")
14 end
15
16 procedure scaleFactor()
17 return tab(any('ED')) || (tab(any('+-')) | "") || tab(many(&digits))
18 end
19
20 procedure scan()
21 local t,c,b
22 static whiteSpace,initIdChars,idChars,hexdigits,commentDepth,commentLineNo
23 initial {
24 /inputFile := &input
25 inputLineNumber := 1
26 inputColumn := 1
27 inputLine := read(inputFile)
28 eoiToken := &null
29 whiteSpace := &ascii[1:34] #control ++ blank
30 initIdChars := &letters
31 hexdigits := &digits ++ 'ABCDEF'
32 idChars := &letters ++ &digits ++ '$_'
33 keywordSet := set([
34 "new",
35 "formal",
36 "label",
37 "tail",
38 "undef",
39 "in",
40 "isb",
41 "isn",
42 "isr",
43 "isl",
44 "isli",
45 "isy",
46 "isp",
47 "isu",
48 "abs",
49 "length",
50 "integer",
51 "real",
52 "logical",
53 "true",
54 "false",
55 "list",
56 "div",
57 "mod",
58 "max",
59 "min",
60 "and",
61 "or",
62 "else",
63 "if",
64 "then",
65 "goto",
66 "out",
67 "begin",
68 "end"
69 ])
70 }
71 if \eoiToken then return eoiToken
72 repeat inputLine ? {
73 tab(inputColumn)
74 tab(many(whiteSpace))
75 c := &pos
76 if b := tab(many(&digits)) then {
77 if b := b || fractionPart() ||
78 scaleFactor() then {
79 t := Token("realN",b,
80 inputLineNumber,c)
81 } else if b ||:= fractionPart() then {
82 t := Token("realN",b,
83 inputLineNumber,c)
84 } else if b ||:= ="." || scaleFactor() then {
85 t := Token("realN",b,
86 inputLineNumber,c)
87 } else {
88 t := Token("integerN",b,
89 inputLineNumber,c)
90 }
91 inputColumn := &pos
92 return t
93 } else
94 if any(initIdChars) then {
95 t := Token("id",tab(many(idChars)),
96 inputLineNumber,c)
97 inputColumn := &pos
98 if member(keywordSet,t.body) then
99 t.type := t.body
100 return t
101 } else
102 if b := =("<-" | ">=" | "<=" | "~=" | "**" ) then {
103 inputColumn := &pos
104 return Token(b,b,inputLineNumber,c)
105 } else
106 if ="(*" then {
107 inputColumn := &pos
108 commentDepth := 1
109 commentLineNo := inputLineNumber
110 while commentDepth > 0 do {
111 tab(upto('*(')|0)
112 if pos(0) then {
113 &pos := 1
114 inputLineNumber +:= 1
115 if not (&subject :=
116 inputLine := read(inputFile))
117 then {
118 eoiToken := Token("EOI","EOI",
119 inputLineNumber,1)
120 write("end of input in comment beginning at ",
121 commentLineNo)
122 return eoiToken
123 }
124 } else if ="*)" then {
125 commentDepth -:= 1
126 } else if ="(*" then {
127 commentDepth +:= 1
128 } else {
129 move(1)
130 }
131 }
132 inputColumn := &pos
133 } else
134 if b := tab(any('\',=()[]~+-*/@&;:><.')) then {
135 inputColumn := &pos
136 return Token(b,b,inputLineNumber,c)
137 } else
138 if pos(0) then {
139 inputColumn := 1
140 inputLineNumber +:= 1
141 if not (inputLine := read(inputFile)) then {
142 eoiToken := Token("EOI","EOI",
143 inputLineNumber,1)
144
145 return eoiToken
146 }
147 } else
148 if ="\"" then {
149 b := tab(find("\""))
150 if not( = "\"" ) then {
151 write("unterminated string at ",
152 inputLineNumber," ",c)
153 }
154 t := Token("symbol",b,inputLineNumber,c)
155 inputColumn := &pos
156 return t
157 } else
158 {
159 write("unexpected character: ",move(1),
160 " at line ",inputLineNumber," column ",c)
161 inputColumn := &pos
162 }
163 }
164 end
165