Next: , Previous: pair, Up: Top


46 parser

     %--------------------------------------------------%
     % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
     %--------------------------------------------------%
     % Copyright (C) 1995-2001, 2003-2008, 2011-2012 The University of Melbourne.
     % This file may only be copied under the terms of the GNU Library General
     % Public License - see the file COPYING.LIB in the Mercury distribution.
     %--------------------------------------------------%
     %
     % File: parser.m.
     % Main author: fjh.
     % Stability: high.
     %
     % This file exports the predicate read_term, which reads
     % a term from the current input stream.
     % The read_term_from_string predicates are the same as the
     % read_term predicates, except that the term is read from
     % a string rather than from the current input stream.
     % The parse_token_list predicate is similar,
     % but it takes a list of tokens rather than a string.
     %
     % The parser and lexer are intended to exactly follow ISO Prolog
     % syntax, but there are some departures from that for three reasons:
     %
     %   (1) I wrote some of the code at home when the ISO Prolog draft
     %       was at uni - so in some places I just guessed.
     %   (2) In some places the lexer reports an error when it shouldn't.
     %   (3) There are a couple of hacks to make it compatible with NU-Prolog
     %       syntax.
     %
     % The parser is a relatively straight-forward top-down recursive descent
     % parser, made somewhat complicated by the need to handle operator
     % precedences.  It uses `lexer.get_token_list' to read a list of tokens.
     % It uses the routines in module `ops' to look up operator precedences.
     %
     %--------------------------------------------------%
     %--------------------------------------------------%
     
     :- module parser.
     :- interface.
     
     :- import_module io.
     :- import_module lexer.
     :- import_module ops.
     :- import_module term_io.
     
     %--------------------------------------------------%
     
         % read_term(Result):
         %
         % Reads a Mercury term from the current input stream.
         %
     :- pred read_term(read_term(T)::out, io::di, io::uo) is det.
     
         % read_term_with_op_table(Result):
         %
         % Reads a term from the current input stream, using the given op_table
         % to interpret the operators.
         %
     :- pred read_term_with_op_table(Ops::in, read_term(T)::out, io::di, io::uo)
         is det <= op_table(Ops).
     
         % read_term_filename(FileName, Result, !IO):
         %
         % Reads a term from the current input stream. The string is the filename
         % to use for the current input stream; this is used in constructing the
         % term.contexts in the read term. This interface is used to support
         % the `:- pragma source_file' directive.
         %
     :- pred read_term_filename(string::in, read_term(T)::out, io::di, io::uo)
         is det.
     
         % read_term_filename_with_op_table(Ops, FileName, Result, !IO):
         %
         % As above but using the given op_table.
         %
     :- pred read_term_filename_with_op_table(Ops::in, string::in,
         read_term(T)::out, io::di, io::uo) is det <= op_table(Ops).
     
     %--------------------------------------------------%
     
         % The read_term_from_string predicates are the same as the read_term
         % predicates, except that the term is read from a string rather than from
         % the current input stream. The returned value `EndPos' is the position
         % one character past the end of the term read. The arguments `MaxOffset'
         % and `StartPos' in the six-argument version specify the length of the
         % string and the position within the string at which to start parsing.
     
         % read_term_from_string(FileName, String, EndPos, Term).
         %
     :- pred read_term_from_string(string::in, string::in, posn::out,
         read_term(T)::out) is det.
     
         % read_term_from_string_with_op_table(Ops, FileName,
         %   String, EndPos, Term).
         %
     :- pred read_term_from_string_with_op_table(Ops::in, string::in,
         string::in, posn::out, read_term(T)::out) is det <= op_table(Ops).
     
         % read_term_from_string(FileName, String, MaxOffset, StartPos,
         %   EndPos, Term).
         %
     :- pred read_term_from_substring(string::in, string::in, int::in,
         posn::in, posn::out, read_term(T)::out) is det.
     
         % read_term_from_string_with_op_table(Ops, FileName, String,
         %   MaxOffset, StartPos, EndPos, Term).
         %
     :- pred read_term_from_substring_with_op_table(Ops::in, string::in,
         string::in, int::in, posn::in, posn::out, read_term(T)::out) is det
         <= op_table(Ops).
     
     %--------------------------------------------------%
     
         % parse_tokens(FileName, TokenList, Result):
         %
     :- pred parse_tokens(string::in, token_list::in, read_term(T)::out) is det.
     
         % parse_tokens(FileName, TokenList, Result):
         %
     :- pred parse_tokens_with_op_table(Ops::in, string::in, token_list::in,
         read_term(T)::out) is det <= op_table(Ops).
     
     %--------------------------------------------------%
     %--------------------------------------------------%