990 | 990 |
| | "d" & "o" & "g" & return 'dog'.
|
991 | 991 |
+ catdog
|
992 | 992 |
= dog
|
|
993 |
|
|
994 |
We can also implement a production scanner with the char scanner. This is
|
|
995 |
more useful.
|
|
996 |
|
|
997 |
| main = program using scanner.
|
|
998 |
| scanner = scan using $.char.
|
|
999 |
| scan = "a" | "b" | "@".
|
|
1000 |
| program = "a" & "@" & "b" & return ok.
|
|
1001 |
+ a@b
|
|
1002 |
= ok
|
|
1003 |
|
|
1004 |
If the production scanner fails to match the input text, it will return an EOF.
|
|
1005 |
This is a little weird, but. Well. Watch this space.
|
|
1006 |
|
|
1007 |
| main = program using scanner.
|
|
1008 |
| scanner = scan using $.char.
|
|
1009 |
| scan = "a" | "b" | "@".
|
|
1010 |
| program = "a" & "@" & "b" & return ok.
|
|
1011 |
+ x
|
|
1012 |
? expected 'a' found 'EOF'
|
|
1013 |
|
|
1014 |
On the other hand, if the scanner understands all the tokens, but the parser
|
|
1015 |
doesn't see the tokens it expects, you get the usual error.
|
|
1016 |
|
|
1017 |
| main = program using scanner.
|
|
1018 |
| scanner = scan using $.char.
|
|
1019 |
| scan = "a" | "b" | "@".
|
|
1020 |
| program = "a" & "@" & "b" & return ok.
|
|
1021 |
+ b@a
|
|
1022 |
? expected 'a' found 'b'
|
|
1023 |
|
|
1024 |
We can write a slightly more realistic scanner, too.
|
|
1025 |
|
|
1026 |
| main = program using scanner.
|
|
1027 |
| scanner = scan using $.char.
|
|
1028 |
| scan = "c" & "a" & "t" & return cat
|
|
1029 |
| | "d" & "o" & "g" & return dog.
|
|
1030 |
| program = "cat" & "dog".
|
|
1031 |
+ catdog
|
|
1032 |
= dog
|
|
1033 |
|
|
1034 |
Parsing using a production scanner ignores any extra text given to it,
|
|
1035 |
just like the built-in parser.
|
|
1036 |
|
|
1037 |
| main = program using scanner.
|
|
1038 |
| scanner = scan using $.char.
|
|
1039 |
| scan = (
|
|
1040 |
| "c" & "a" & "t" & return cat | "d" & "o" & "g" & return dog
|
|
1041 |
| ).
|
|
1042 |
| program = "cat" & "dog".
|
|
1043 |
+ catdogfoobar
|
|
1044 |
= dog
|
|
1045 |
|
|
1046 |
Herein lie an excessive number of tests that I wrote while I was debugging.
|
|
1047 |
Some of them will be cleaned up at a future point.
|
|
1048 |
|
|
1049 |
| main = program using scanner.
|
|
1050 |
| scanner = scan using $.char.
|
|
1051 |
| scan = (
|
|
1052 |
| "c" & "a" & "t" & return cat | "d" & "o" & "g" & return dog
|
|
1053 |
| ).
|
|
1054 |
| program = "cat" & print 1 &
|
|
1055 |
| ("cat" & print 2 | "dog" & print 3) &
|
|
1056 |
| "dog" & print 4 & return ok.
|
|
1057 |
+ catcatdog
|
|
1058 |
= 1
|
|
1059 |
= 2
|
|
1060 |
= 4
|
|
1061 |
= ok
|
|
1062 |
|
|
1063 |
| main = program using scanner.
|
|
1064 |
| scanner = scan using $.char.
|
|
1065 |
| scan = (
|
|
1066 |
| "c" & "a" & "t" & return cat | "d" & "o" & "g" & return dog
|
|
1067 |
| ).
|
|
1068 |
| program = "cat" & print 1 &
|
|
1069 |
| ("cat" & print 2 | "dog" & print 3) &
|
|
1070 |
| "dog" & print 4 & return ok.
|
|
1071 |
+ catdogdog
|
|
1072 |
= 1
|
|
1073 |
= 3
|
|
1074 |
= 4
|
|
1075 |
= ok
|
993 | 1076 |
|
994 | 1077 |
Three good ways to shoot yourself in the foot
|
995 | 1078 |
---------------------------------------------
|