| 1 |
# Scanner for the input language used by TCLL1, |
| 2 |
# an LL(1) parser generator). |
| 3 |
# (written by Dr. Thomas W. Christopher) |
| 4 |
# |
| 5 |
global inputFile |
| 6 |
global inputLine,inputLineNumber,inputColumn,eoiToken |
| 7 |
global tokenTypes |
| 8 |
|
| 9 |
procedure initScanner(filename) |
| 10 |
inputFile := open(filename,"r") | fail |
| 11 |
return |
| 12 |
end |
| 13 |
|
| 14 |
procedure scan() |
| 15 |
local t,c,b |
| 16 |
static whiteSpace,initIdChars,idChars |
| 17 |
initial { |
| 18 |
/inputFile:=&input |
| 19 |
inputLineNumber:=0 |
| 20 |
inputColumn:=1 |
| 21 |
inputLine:="" |
| 22 |
eoiToken:=&null |
| 23 |
whiteSpace:=&ascii[1:34] #control ++ blank |
| 24 |
initIdChars := &letters ++ '_' |
| 25 |
idChars := &letters ++ &digits ++ '_' |
| 26 |
tokenTypes := table() |
| 27 |
t := [ ".","DOT", |
| 28 |
":","COLON", |
| 29 |
"=","EQ", |
| 30 |
"|","BAR", |
| 31 |
"(","LPAR", |
| 32 |
")","RPAR", |
| 33 |
"[","LBRACK", |
| 34 |
"]","RBRACK", |
| 35 |
"{","LBRACE", |
| 36 |
"}","RBRACE", |
| 37 |
"!","BANG"] |
| 38 |
while tokenTypes[get(t)] := get(t) |
| 39 |
} |
| 40 |
if \eoiToken then return eoiToken |
| 41 |
repeat inputLine ? { |
| 42 |
tab(inputColumn) |
| 43 |
tab(many(whiteSpace)) |
| 44 |
c := &pos |
| 45 |
if any(initIdChars) then { |
| 46 |
t := Token("ID",tab(many(idChars)), |
| 47 |
inputLineNumber,c) |
| 48 |
inputColumn := &pos |
| 49 |
return t |
| 50 |
} else |
| 51 |
if b := tab(any('.:=()[]{}|!')) then { |
| 52 |
inputColumn := &pos |
| 53 |
return Token(tokenTypes[b],b,inputLineNumber,c) |
| 54 |
} else |
| 55 |
if ="#" | pos(0) then { |
| 56 |
inputColumn := 1 |
| 57 |
inputLineNumber +:= 1 |
| 58 |
if not (inputLine := read(inputFile)) then { |
| 59 |
eoiToken := Token("EOI","EOI", |
| 60 |
inputLineNumber,1) |
| 61 |
return eoiToken |
| 62 |
} |
| 63 |
} else |
| 64 |
if ="\"" then { |
| 65 |
if t := Token("ID",tab(find("\"")), |
| 66 |
inputLineNumber,c) then { |
| 67 |
move(1) |
| 68 |
} else { |
| 69 |
write("unterminated quote at ", |
| 70 |
inputLineNumber," ",c) |
| 71 |
t:=Token("ID",tab(many(~whiteSpace)), |
| 72 |
inputLineNumber,c) |
| 73 |
} |
| 74 |
inputColumn := &pos |
| 75 |
return t |
| 76 |
} else |
| 77 |
{ |
| 78 |
write("unexpected character: ",move(1), |
| 79 |
" at ",inputLineNumber," ",c) |
| 80 |
inputColumn := &pos |
| 81 |
} |
| 82 |
} |
| 83 |
end |
| 84 |
|
| 85 |
|