(* Copyright (C) 2005, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://helm.cs.unibo.it/ *) open Printf exception Parse_error of Token.flocation * string module Level1Lexer = struct type te = string * string let lexer = CicNotationLexer.syntax_pattern_lexer end module Level1Parser = Grammar.GMake (Level1Lexer) module Level2Lexer = struct type te = string * string let lexer = CicNotationLexer.ast_pattern_lexer end module Level2Parser = Grammar.GMake (Level2Lexer) module Level3Lexer = struct type te = string * string let lexer = CicNotationLexer.ast_pattern_lexer end module Level3Parser = Grammar.GMake (Level3Lexer) let level1_pattern = Level1Parser.Entry.create "level1_pattern" let level2_pattern = Level2Parser.Entry.create "level2_pattern" let level3_interpretation = Level3Parser.Entry.create "level3_interpretation" let return_term loc term = () (*let fail floc msg =*) (* let (x, y) = CicAst.loc_of_floc floc in*) (* failwith (sprintf "Error at characters %d - %d: %s" x y msg)*) let int_of_string s = try Pervasives.int_of_string s with Failure _ -> failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s) (* {{{ Grammar for concrete syntax patterns, notation level 1 *) GEXTEND Level1Parser GLOBAL: level1_pattern; level1_pattern: [ [ p = pattern -> () ] ]; pattern: [ [ p = LIST1 simple_pattern -> () ] ]; literal: [ [ s = SYMBOL -> () | k = KEYWORD -> () ] ]; sep: [ [ SYMBOL "\\SEP"; sep = literal -> () ] ]; row_sep: [ [ SYMBOL "\\ROWSEP"; sep = literal -> () ] ]; field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ]; box_pattern: [ [ SYMBOL "\\HBOX"; p = simple_pattern -> () | SYMBOL "\\VBOX"; p = simple_pattern -> () | SYMBOL "\\BREAK" -> () ] ]; magic_pattern: [ [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> () | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> () | SYMBOL "\\OPT"; p = simple_pattern -> () ] ]; pattern_variable: [ [ id = IDENT -> () | SYMBOL "\\NUM"; id = IDENT -> () | SYMBOL "\\IDENT"; id = IDENT -> () ] ]; simple_pattern: [ "layout" LEFTA [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF -> () | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF -> () | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF -> () | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF -> () | SYMBOL "["; p1 = pattern; SYMBOL "\\OVER"; p2 = pattern; SYMBOL "]" -> () | SYMBOL "["; p1 = pattern; SYMBOL "\\ATOP"; p2 = pattern; SYMBOL "]" -> () | SYMBOL "\\ARRAY"; p = SELF; fsep = OPT field_sep; rsep = OPT row_sep -> () | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF -> () | SYMBOL "\\SQRT"; p = SELF -> () | SYMBOL "\\ROOT"; arg = pattern; SYMBOL "\\OF"; index = SELF -> () ] | "simple" NONA [ m = magic_pattern -> () | v = pattern_variable -> () | b = box_pattern -> () | n = NUMBER -> () | SYMBOL "["; p = pattern; SYMBOL "]" -> () ] ]; END (* }}} *) (* {{{ Grammar for ast patterns, notation level 2 *) GEXTEND Level2Parser GLOBAL: level2_pattern; level2_pattern: [ [ p = pattern -> () ] ]; sort: [ [ SYMBOL "\\PROP" -> () | SYMBOL "\\SET" -> () | SYMBOL "\\TYPE" -> () ] ]; explicit_subst: [ [ (* TODO explicit substitution *) ] ]; meta_subst: [ [ (* TODO meta substitution *) ] ]; possibly_typed_name: [ [ SYMBOL "("; i = IDENT; SYMBOL ":"; typ = pattern; SYMBOL ")" -> () | i = IDENT -> () ] ]; match_pattern: [ [ n = IDENT -> () | SYMBOL "("; head = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" -> () ] ]; binder: [ [ SYMBOL <:unicode> (* Π *) -> () | SYMBOL <:unicode> (* ∃ *) -> () | SYMBOL <:unicode> (* ∀ *) -> () | SYMBOL <:unicode> (* λ *) -> () ] ]; bound_names: [ [ vars = LIST1 IDENT SEP SYMBOL ","; typ = OPT [ SYMBOL ":"; p = pattern -> () ] -> () | LIST1 [ SYMBOL "("; vars = LIST1 IDENT SEP SYMBOL ","; typ = OPT [ SYMBOL ":"; p = pattern -> () ]; SYMBOL ")" -> () ] -> () ] ]; induction_kind: [ [ IDENT "rec" -> () | IDENT "corec" -> () ] ]; let_defs: [ [ defs = LIST1 [ name = IDENT; args = bound_names; index_name = OPT [ IDENT "on"; idx = IDENT -> () ]; ty = OPT [ SYMBOL ":" ; p = pattern -> () ]; SYMBOL <:unicode> (* ≝ *); body = pattern -> () ] SEP IDENT "and" -> () ] ]; pattern_variable: [ [ SYMBOL "\\NUM"; id = IDENT -> () | SYMBOL "\\IDENT"; id = IDENT -> () | SYMBOL "\\FRESH"; id = IDENT -> () ] ]; magic_pattern: [ [ SYMBOL "\\FOLD"; n = OPT NUMBER; [ IDENT "left" | IDENT "right" ]; LIST1 IDENT; SYMBOL "."; p1 = pattern; OPT [ SYMBOL "\\LAMBDA"; LIST1 IDENT ]; p2 = pattern -> () | SYMBOL "\\DEFAULT"; id = IDENT; p1 = pattern; p2 = pattern -> () ] ]; pattern: [ "letin" NONA [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode> (* ≝ *); p1 = pattern; "in"; p2 = pattern -> () | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in"; body = pattern -> () ] | "binder" RIGHTA [ b = binder; bound_names; SYMBOL "."; body = pattern -> () ] | "extension" [ ] | "apply" LEFTA [ p1 = pattern; p2 = pattern -> () ] | "simple" NONA [ i = IDENT -> () | i = IDENT; s = explicit_subst -> () | n = NUMBER -> () | IMPLICIT -> () | m = META -> () | m = META; s = meta_subst -> () | s = sort -> () | s = SYMBOL -> () | u = URI -> () | outtyp = OPT [ SYMBOL "["; typ = pattern; SYMBOL "]" ]; IDENT "match"; t = pattern; indty_ident = OPT [ SYMBOL ":"; id = IDENT ]; IDENT "with"; SYMBOL "["; patterns = LIST0 [ lhs = match_pattern; SYMBOL <:unicode> (* ⇒ *); rhs = pattern -> () ] SEP SYMBOL "|"; SYMBOL "]" -> () | SYMBOL "("; p1 = pattern; SYMBOL ":"; p2 = pattern; SYMBOL ")" -> () | SYMBOL "("; p = pattern; SYMBOL ")" -> () | v = pattern_variable -> () | m = magic_pattern -> () ] ]; END (* }}} *) (* {{{ Grammar for interpretation, notation level 3 *) GEXTEND Level3Parser GLOBAL: level3_interpretation; level3_interpretation: [ [ i = interpretation -> () ] ]; argument: [ [ i = IDENT -> () | SYMBOL <:unicode> (* η *); SYMBOL "."; a = SELF -> () | SYMBOL <:unicode> (* η *); i = IDENT; SYMBOL "."; a = SELF -> () ] ]; term: [ [ u = URI -> () | a = argument -> () | SYMBOL "("; terms = LIST1 SELF; SYMBOL ")" -> () ] ]; interpretation: [ [ IDENT "interpretation"; s = SYMBOL; args = LIST1 argument; IDENT "as"; t = term -> () ] ]; END (* }}} *) let exc_located_wrapper f = try f () with | Stdpp.Exc_located (floc, Stream.Error msg) -> raise (Parse_error (floc, msg)) | Stdpp.Exc_located (floc, exn) -> raise (Parse_error (floc, (Printexc.to_string exn))) let parse_syntax_pattern stream = exc_located_wrapper (fun () -> (Level1Parser.Entry.parse level1_pattern (Level1Parser.parsable stream))) let parse_ast_pattern stream = exc_located_wrapper (fun () -> (Level2Parser.Entry.parse level2_pattern (Level2Parser.parsable stream))) let parse_interpretation stream = exc_located_wrapper (fun () -> (Level3Parser.Entry.parse level3_interpretation (Level3Parser.parsable stream))) (* vim:set encoding=utf8 foldmethod=marker: *)