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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#############################################################################
##
#W parse.gi OpenMath Package Andrew Solomon
#W Marco Costantini
##
#Y Copyright (C) 1999, 2000, 2001, 2006
#Y School Math and Comp. Sci., University of St. Andrews, Scotland
#Y Copyright (C) 2004, 2005, 2006 Marco Costantini
##
## The parser reads token/values off the stream and builds GAP objects.
##
InstallGlobalFunction(OMgetObjectByteStream, function(inputstream)
local stream, obj;
# assumes there is at least one byte on stream.
stream := rec(next := ReadByte(inputstream), stream := inputstream);
# Start -> OMtokenObject object OMtokenEndObject
# get the OMtokenObject
OMgetToken(stream, OMtokenObject);
# parse the object
obj := OMparseObject(stream);
OMgetToken(stream, OMtokenEndObject);
return obj;
end);
InstallGlobalFunction(OMparseApplication, function(stream)
local head, rest, nextob;
# first get the OMtokenApp off the stream
OMgetToken(stream, OMtokenApp);
#
# 1. Get the "head" object
# the possibilities are:
#
# 2. Apply the head to the rest
#
# 1.1 If it's a symbol, look it up and see what it translates to in GAP
if OMnextToken(stream) = OMtokenSymbol then
head := OMsymLookup(OMgetSymbol(stream));
else
head := OMparseObject(stream);
fi;
# get the rest
rest := [];
while OMnextToken(stream) <> OMtokenEndApp do
nextob := OMparseObject(stream);
Append(rest, [nextob]);
od;
# finally get the OMtokenEndApp off the stream
OMgetToken(stream, OMtokenEndApp);
return head( rest );
end);
# just ignore everything but the object
InstallGlobalFunction(OMparseAttribution, function(stream)
local nextob, ob;
# first get the OMtokenAttr off the stream
OMgetToken(stream, OMtokenAttr);
# now get the OMtokenAtp off the stream
OMgetToken(stream, OMtokenAtp);
# just read all the objects until the end of attribution
while OMnextToken(stream) <> OMtokenEndAtp do
nextob := OMparseObject(stream);
od;
# get the OMtokenEndAtp off the stream
OMgetToken(stream, OMtokenEndAtp);
# finally, the only thing we don't ignore - the unattributed object
ob := OMparseObject(stream);
OMgetToken(stream, OMtokenEndAttr);
return ob;
end);
InstallGlobalFunction(OMparseBind, function(stream)
Error("Binding is unimplemented");
end);
InstallGlobalFunction(OMparseObject, function(stream)
##
## Object -> symbol | variable | integer | float | string | bytearray |
## application | binding | error | attribution
# first the basic objects
if OMnextToken(stream) = OMtokenSymbol then
# this is just a nullary symbol
return OMsymLookup(OMgetSymbol(stream));
elif OMnextToken(stream) = OMtokenVar then
return OMgetVar(stream);
elif OMnextToken(stream) = OMtokenInteger then
return OMgetInteger(stream);
elif OMnextToken(stream) = OMtokenFloat then
Error("GAP doesn't support floating point numbers.");
elif OMnextToken(stream) = OMtokenString then
return OMgetString(stream);
elif OMnextToken(stream) = OMtokenByteArray then
Error("GAP doesn't support byte arrays.");
# now the compound objects
elif OMnextToken(stream) = OMtokenApp then
return OMparseApplication(stream);
elif OMnextToken(stream) = OMtokenBind then
return OMparseBind(stream);
elif OMnextToken(stream) = OMtokenError then
Error("OMtokenError encountered");
elif OMnextToken(stream) = OMtokenAttr then
return OMparseAttribution(stream);
else
Error("unrecognized token ", OMnextToken(stream));
fi;
end);
#############################################################################
#E
|