Exemplo n.º 1
0
        private ATerm parseAbbrev(ATermReader reader)         // throws IOException
        {
            ATerm result;
            int   abbrev;

            int c = reader.read();

            abbrev = 0;
            while (isBase64(c))
            {
                abbrev *= 64;
                if (c >= 'A' && c <= 'Z')
                {
                    abbrev += c - 'A';
                }
                else if (c >= 'a' && c <= 'z')
                {
                    abbrev += c - 'a' + 26;
                }
                else if (c >= '0' && c <= '9')
                {
                    abbrev += c - '0' + 52;
                }
                else if (c == '+')
                {
                    abbrev += 62;
                }
                else if (c == '/')
                {
                    abbrev += 63;
                }
                else
                {
                    throw new Exception("not a base-64 digit: " + c);
                }

                c = reader.read();
            }

            result = reader.getTerm(abbrev);

            return(result);
        }
Exemplo n.º 2
0
        private string parseId(ATermReader reader)         // throws IOException
        {
            int           c   = reader.getLastChar();
            StringBuilder buf = new StringBuilder(32);

            do
            {
                buf.Append((char)c);
                c = reader.read();
            }while (Char.IsLetterOrDigit((char)c) || c == '_' || c == '-');

            return(buf.ToString());
        }
Exemplo n.º 3
0
		/* lock  */
		private ATerm parseFromReader(ATermReader reader) // throws IOException 
		{
			ATerm result;
			int c, start, end;
			string funname;

			start = reader.getPosition();
			switch (reader.getLastChar()) 
			{
				case -1 :
					throw new ParseError("premature EOF encountered.");
					
				case '#' :
					return parseAbbrev(reader);

				case '[' :

					c = reader.readSkippingWS();
					if (c == -1) 
					{
						throw new ParseError("premature EOF encountered.");
					}

					if (c == ']') 
					{
						c = reader.readSkippingWS();
						result = (ATerm) empty;
					}
					else 
					{
						result = parseATerms(reader);
						if (reader.getLastChar() != ']') 
						{
							throw new ParseError("expected ']' but got '" + (char) reader.getLastChar() + "'");
						}
						c = reader.readSkippingWS();
					}

					break;

				case '<' :

					c = reader.readSkippingWS();
					ATerm ph = parseFromReader(reader);

					if (reader.getLastChar() != '>') 
					{
						throw new ParseError("expected '>' but got '" + (char) reader.getLastChar() + "'");
					}

					c = reader.readSkippingWS();

					result = makePlaceholder(ph);

					break;

				case '"' :

					funname = parseString(reader);

					c = reader.readSkippingWS();
					if (reader.getLastChar() == '(') 
					{
						c = reader.readSkippingWS();
						if (c == -1) 
						{
							throw new ParseError("premature EOF encountered.");
						}
						if (reader.getLastChar() == ')') 
						{
							result = makeAppl(makeAFun(funname, 0, true));
						}
						else 
						{
							ATerm[] list = parseATermsArray(reader);

							if (reader.getLastChar() != ')') 
							{
								throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
							}
							result = makeAppl(makeAFun(funname, list.Length, true), list);
						}
						c = reader.readSkippingWS();
						if (c == -1) 
						{
							throw new ParseError("premature EOF encountered.");
						}
					}
					else 
					{
						result = makeAppl(makeAFun(funname, 0, true));
					}

					break;

				case '(' :

					c = reader.readSkippingWS();
					if (c == -1) 
					{
						throw new ParseError("premature EOF encountered.");
					}
					if (reader.getLastChar() == ')') 
					{
						result = makeAppl(makeAFun("", 0, false));
					}
					else 
					{
						ATerm[] list = parseATermsArray(reader);

						if (reader.getLastChar() != ')') 
						{
							throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
						}
						result = makeAppl(makeAFun("", list.Length, false), list);
					}
					c = reader.readSkippingWS();
					if (c == -1) 
					{
						throw new ParseError("premature EOF encountered.");
					}

					break;

				case '-' : goto case '9';
				case '0' : goto case '9';
				case '1' : goto case '9';
				case '2' : goto case '9';
				case '3' : goto case '9';
				case '4' : goto case '9';
				case '5' : goto case '9';
				case '6' : goto case '9';
				case '7' : goto case '9';
				case '8' : goto case '9';
				case '9' :
					result = parseNumber(reader);
					c = reader.skipWS();
					break;

				default :
					c = reader.getLastChar();
					if (Char.IsLetter((char) c)) 
					{

						funname = parseId(reader);
						c = reader.skipWS();
						if (reader.getLastChar() == '(') 
						{
							c = reader.readSkippingWS();
							if (c == -1) 
							{
								throw new ParseError("premature EOF encountered.");
							}
							if (reader.getLastChar() == ')') 
							{
								result = makeAppl(makeAFun(funname, 0, false));
							}
							else 
							{
								ATerm[] list = parseATermsArray(reader);

								if (reader.getLastChar() != ')') 
								{
									throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
								}
								result = makeAppl(makeAFun(funname, list.Length, false), list);
							}
							c = reader.readSkippingWS();
						}
						else 
						{
							result = makeAppl(makeAFun(funname, 0, false));
						}

					}
					else 
					{
						throw new ParseError("illegal character: " + reader.getLastChar());
					}
					break;
			}

			if (reader.getLastChar() == '{') 
			{

				ATermList annos;
				if (reader.readSkippingWS() == '}') 
				{
					reader.readSkippingWS();
					annos = empty;
				}
				else 
				{
					annos = parseATerms(reader);
					if (reader.getLastChar() != '}') 
					{
						throw new ParseError("'}' expected");
					}
					reader.readSkippingWS();
				}
				result = result.setAnnotations(annos);

			}

			/* Parse some ToolBus anomalies for backwards compatibility */
			if (reader.getLastChar() == ':') 
			{
				reader.read();
				ATerm anno = parseFromReader(reader);
				result = result.setAnnotation(parse("type"), anno);
			}

			if (reader.getLastChar() == '?') 
			{
				reader.readSkippingWS();
				result = result.setAnnotation(parse("result"), parse("true"));
			}

			end = reader.getPosition();
			reader.storeNextTerm(result, end - start);

			return result;
		}
Exemplo n.º 4
0
		private string parseString(ATermReader reader) // throws IOException 
		{
			bool escaped;
			StringBuilder str = new StringBuilder();

			do 
			{
				escaped = false;
				if (reader.read() == '\\') 
				{
					reader.read();
					escaped = true;
				}

				if (escaped) 
				{
					switch (reader.getLastChar()) 
					{
						case 'n' :
							str.Append('\n');
							break;
						case 't' :
							str.Append('\t');
							break;
						case 'b' :
							str.Append('\b');
							break;
						case 'r' :
							str.Append('\r');
							break;
						case 'f' :
							str.Append('\f');
							break;
						case '\\' :
							str.Append('\\');
							break;
						case '\'' :
							str.Append('\'');
							break;
						case '\"' :
							str.Append('\"');
							break;
						case '0' : goto case '7';
						case '1' : goto case '7';
						case '2' : goto case '7';
						case '3' : goto case '7';
						case '4' : goto case '7';
						case '5' : goto case '7';
						case '6' : goto case '7';
						case '7' :
							str.Append(reader.readOct());
							break;
						default :
							str.Append('\\').Append((char) reader.getLastChar());
							break;
					}
				}
				else if (reader.getLastChar() != '\"')
					str.Append((char) reader.getLastChar());
			}
			while (escaped || reader.getLastChar() != '"');

			return str.ToString();
		}
Exemplo n.º 5
0
		private string parseId(ATermReader reader) // throws IOException 
		{
			int c = reader.getLastChar();
			StringBuilder buf = new StringBuilder(32);

			do 
			{
				buf.Append((char) c);
				c = reader.read();
			}
			while (Char.IsLetterOrDigit((char) c) || c == '_' || c == '-');

			return buf.ToString();
		}
Exemplo n.º 6
0
		private ATerm parseNumber(ATermReader reader) // throws IOException 
		{
			StringBuilder str = new StringBuilder();
			ATerm result;

			do 
			{
				str.Append((char) reader.getLastChar());
			}
			while (Char.IsDigit((char) reader.read()));

			if (reader.getLastChar() != '.' && reader.getLastChar() != 'e' && reader.getLastChar() != 'E') 
			{
				int val;
				try 
				{
					val = int.Parse(str.ToString());
				}
				catch (FormatException) 
				{
					throw new ParseError("malformed int");
				}
				result = makeInt(val);
			}
			else 
			{
				if (reader.getLastChar() == '.') 
				{
					str.Append('.');
					reader.read();
					if (!Char.IsDigit((char) reader.getLastChar()))
						throw new ParseError("digit expected");
					do 
					{
						str.Append((char) reader.getLastChar());
					}
					while (Char.IsDigit((char) reader.read()));
				}
				if (reader.getLastChar() == 'e' || reader.getLastChar() == 'E') 
				{
					str.Append((char) reader.getLastChar());
					reader.read();
					if (reader.getLastChar() == '-' || reader.getLastChar() == '+') 
					{
						str.Append((char) reader.getLastChar());
						reader.read();
					}
					if (!Char.IsDigit((char) reader.getLastChar()))
						throw new ParseError("digit expected!");
					do 
					{
						str.Append((char) reader.getLastChar());
					}
					while (Char.IsDigit((char) reader.read()));
				}
				double val;
				try 
				{
					val = double.Parse(str.ToString());
					//					Double.valueOf(str.ToString()).doubleValue();
				}
				catch (FormatException) 
				{
					throw new ParseError("malformed real");
				}
				result = makeReal(val);
			}
			return result;
		}
Exemplo n.º 7
0
		private ATerm parseAbbrev(ATermReader reader) // throws IOException 
		{
			ATerm result;
			int abbrev;

			int c = reader.read();

			abbrev = 0;
			while (isBase64(c)) 
			{
				abbrev *= 64;
				if (c >= 'A' && c <= 'Z') 
				{
					abbrev += c - 'A';
				}
				else if (c >= 'a' && c <= 'z') 
				{
					abbrev += c - 'a' + 26;
				}
				else if (c >= '0' && c <= '9') 
				{
					abbrev += c - '0' + 52;
				}
				else if (c == '+') 
				{
					abbrev += 62;
				}
				else if (c == '/') 
				{
					abbrev += 63;
				}
				else 
				{
					throw new Exception("not a base-64 digit: " + c);
				}

				c = reader.read();
			}

			result = reader.getTerm(abbrev);

			return result;
		}
Exemplo n.º 8
0
        /* lock  */
        private ATerm parseFromReader(ATermReader reader)         // throws IOException
        {
            ATerm  result;
            int    c, start, end;
            string funname;

            start = reader.getPosition();
            switch (reader.getLastChar())
            {
            case -1:
                throw new ParseError("premature EOF encountered.");

            case '#':
                return(parseAbbrev(reader));

            case '[':

                c = reader.readSkippingWS();
                if (c == -1)
                {
                    throw new ParseError("premature EOF encountered.");
                }

                if (c == ']')
                {
                    c      = reader.readSkippingWS();
                    result = (ATerm)empty;
                }
                else
                {
                    result = parseATerms(reader);
                    if (reader.getLastChar() != ']')
                    {
                        throw new ParseError("expected ']' but got '" + (char)reader.getLastChar() + "'");
                    }
                    c = reader.readSkippingWS();
                }

                break;

            case '<':

                c = reader.readSkippingWS();
                ATerm ph = parseFromReader(reader);

                if (reader.getLastChar() != '>')
                {
                    throw new ParseError("expected '>' but got '" + (char)reader.getLastChar() + "'");
                }

                c = reader.readSkippingWS();

                result = makePlaceholder(ph);

                break;

            case '"':

                funname = parseString(reader);

                c = reader.readSkippingWS();
                if (reader.getLastChar() == '(')
                {
                    c = reader.readSkippingWS();
                    if (c == -1)
                    {
                        throw new ParseError("premature EOF encountered.");
                    }
                    if (reader.getLastChar() == ')')
                    {
                        result = makeAppl(makeAFun(funname, 0, true));
                    }
                    else
                    {
                        ATerm[] list = parseATermsArray(reader);

                        if (reader.getLastChar() != ')')
                        {
                            throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
                        }
                        result = makeAppl(makeAFun(funname, list.Length, true), list);
                    }
                    c = reader.readSkippingWS();
                    if (c == -1)
                    {
                        throw new ParseError("premature EOF encountered.");
                    }
                }
                else
                {
                    result = makeAppl(makeAFun(funname, 0, true));
                }

                break;

            case '(':

                c = reader.readSkippingWS();
                if (c == -1)
                {
                    throw new ParseError("premature EOF encountered.");
                }
                if (reader.getLastChar() == ')')
                {
                    result = makeAppl(makeAFun("", 0, false));
                }
                else
                {
                    ATerm[] list = parseATermsArray(reader);

                    if (reader.getLastChar() != ')')
                    {
                        throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
                    }
                    result = makeAppl(makeAFun("", list.Length, false), list);
                }
                c = reader.readSkippingWS();
                if (c == -1)
                {
                    throw new ParseError("premature EOF encountered.");
                }

                break;

            case '-': goto case '9';

            case '0': goto case '9';

            case '1': goto case '9';

            case '2': goto case '9';

            case '3': goto case '9';

            case '4': goto case '9';

            case '5': goto case '9';

            case '6': goto case '9';

            case '7': goto case '9';

            case '8': goto case '9';

            case '9':
                result = parseNumber(reader);
                c      = reader.skipWS();
                break;

            default:
                c = reader.getLastChar();
                if (Char.IsLetter((char)c))
                {
                    funname = parseId(reader);
                    c       = reader.skipWS();
                    if (reader.getLastChar() == '(')
                    {
                        c = reader.readSkippingWS();
                        if (c == -1)
                        {
                            throw new ParseError("premature EOF encountered.");
                        }
                        if (reader.getLastChar() == ')')
                        {
                            result = makeAppl(makeAFun(funname, 0, false));
                        }
                        else
                        {
                            ATerm[] list = parseATermsArray(reader);

                            if (reader.getLastChar() != ')')
                            {
                                throw new ParseError("expected ')' but got '" + reader.getLastChar() + "'");
                            }
                            result = makeAppl(makeAFun(funname, list.Length, false), list);
                        }
                        c = reader.readSkippingWS();
                    }
                    else
                    {
                        result = makeAppl(makeAFun(funname, 0, false));
                    }
                }
                else
                {
                    throw new ParseError("illegal character: " + reader.getLastChar());
                }
                break;
            }

            if (reader.getLastChar() == '{')
            {
                ATermList annos;
                if (reader.readSkippingWS() == '}')
                {
                    reader.readSkippingWS();
                    annos = empty;
                }
                else
                {
                    annos = parseATerms(reader);
                    if (reader.getLastChar() != '}')
                    {
                        throw new ParseError("'}' expected");
                    }
                    reader.readSkippingWS();
                }
                result = result.setAnnotations(annos);
            }

            /* Parse some ToolBus anomalies for backwards compatibility */
            if (reader.getLastChar() == ':')
            {
                reader.read();
                ATerm anno = parseFromReader(reader);
                result = result.setAnnotation(parse("type"), anno);
            }

            if (reader.getLastChar() == '?')
            {
                reader.readSkippingWS();
                result = result.setAnnotation(parse("result"), parse("true"));
            }

            end = reader.getPosition();
            reader.storeNextTerm(result, end - start);

            return(result);
        }
Exemplo n.º 9
0
        private string parseString(ATermReader reader)         // throws IOException
        {
            bool          escaped;
            StringBuilder str = new StringBuilder();

            do
            {
                escaped = false;
                if (reader.read() == '\\')
                {
                    reader.read();
                    escaped = true;
                }

                if (escaped)
                {
                    switch (reader.getLastChar())
                    {
                    case 'n':
                        str.Append('\n');
                        break;

                    case 't':
                        str.Append('\t');
                        break;

                    case 'b':
                        str.Append('\b');
                        break;

                    case 'r':
                        str.Append('\r');
                        break;

                    case 'f':
                        str.Append('\f');
                        break;

                    case '\\':
                        str.Append('\\');
                        break;

                    case '\'':
                        str.Append('\'');
                        break;

                    case '\"':
                        str.Append('\"');
                        break;

                    case '0': goto case '7';

                    case '1': goto case '7';

                    case '2': goto case '7';

                    case '3': goto case '7';

                    case '4': goto case '7';

                    case '5': goto case '7';

                    case '6': goto case '7';

                    case '7':
                        str.Append(reader.readOct());
                        break;

                    default:
                        str.Append('\\').Append((char)reader.getLastChar());
                        break;
                    }
                }
                else if (reader.getLastChar() != '\"')
                {
                    str.Append((char)reader.getLastChar());
                }
            }while (escaped || reader.getLastChar() != '"');

            return(str.ToString());
        }
Exemplo n.º 10
0
        private ATerm parseNumber(ATermReader reader)         // throws IOException
        {
            StringBuilder str = new StringBuilder();
            ATerm         result;

            do
            {
                str.Append((char)reader.getLastChar());
            }while (Char.IsDigit((char)reader.read()));

            if (reader.getLastChar() != '.' && reader.getLastChar() != 'e' && reader.getLastChar() != 'E')
            {
                int val;
                try
                {
                    val = int.Parse(str.ToString());
                }
                catch (FormatException)
                {
                    throw new ParseError("malformed int");
                }
                result = makeInt(val);
            }
            else
            {
                if (reader.getLastChar() == '.')
                {
                    str.Append('.');
                    reader.read();
                    if (!Char.IsDigit((char)reader.getLastChar()))
                    {
                        throw new ParseError("digit expected");
                    }
                    do
                    {
                        str.Append((char)reader.getLastChar());
                    }while (Char.IsDigit((char)reader.read()));
                }
                if (reader.getLastChar() == 'e' || reader.getLastChar() == 'E')
                {
                    str.Append((char)reader.getLastChar());
                    reader.read();
                    if (reader.getLastChar() == '-' || reader.getLastChar() == '+')
                    {
                        str.Append((char)reader.getLastChar());
                        reader.read();
                    }
                    if (!Char.IsDigit((char)reader.getLastChar()))
                    {
                        throw new ParseError("digit expected!");
                    }
                    do
                    {
                        str.Append((char)reader.getLastChar());
                    }while (Char.IsDigit((char)reader.read()));
                }
                double val;
                try
                {
                    val = double.Parse(str.ToString());
                    //					Double.valueOf(str.ToString()).doubleValue();
                }
                catch (FormatException)
                {
                    throw new ParseError("malformed real");
                }
                result = makeReal(val);
            }
            return(result);
        }