Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

110 lines (86 sloc) 4.121 kb
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
{{
open Parser_utils
}}
(**
{6 Auxiliary functions}
*)
let deco v = (=Opa_parser.deco(v));
pos <- Opa_parser.pos
spacing <- Opa_lexer.spacing
ml_identifier <- Opa_parser.ml_identifier
deco_ml_identifier <- Opa_parser.deco_ml_identifier
(**
{6 Main grammar}
*)
;Expression = deco(Expression_aux)
Expression_aux <- Opa_lexer.BAR? ParserSeq:e (Opa_lexer.BAR ParserSeq:e {{e}})*:l {{ Trx_ast.Expr (e::l) }}
ExpressionAsFunction <- Opa_lexer.PARSER Expression:expr Opa_lexer.END? {{ expr }}
Item = deco(Item_aux)
Item_aux <-
/ SubruleName?:name Prefix:prefix Primary:primary Suffix:suffix
{{ {Trx_ast.item_name=name; Trx_ast.item_prefix=prefix; Trx_ast.item_primary=primary; Trx_ast.item_suffix=suffix} }}
/ [~] (=deco(Identifier:e {{e}})):e Suffix:suffix
(* the only place when ~ is allowed is when saying ~p or ~X.p*, but not ~{X.p}, not ~[abc], etc. *)
{{ let (((name,_),e),label) = e in
{Trx_ast.item_name=Some name; Trx_ast.item_prefix=`NORMAL; Trx_ast.item_primary=(Trx_ast.Rule e,label); Trx_ast.item_suffix=suffix} }}
ParserSeq = deco(ParserSeq_aux)
ParserSeq_aux <- pos:pos Item+:items ParserCode?:pc {{ {Trx_ast.seq_items=items; Trx_ast.seq_code=pc} }}
ParserCode <- Opa_lexer.rarrow Opa_parser.expr:e {{ e }}
SubruleName <- ml_identifier:v EQUAL {{ v }}
PrefixElement <- AND {{ `AND }}
/ NOT {{ `NOT }}
SuffixElement <- QUESTION {{ `QUESTION }}
/ STAR {{ `STAR }}
/ PLUS {{ `PLUS }}
Prefix <- PrefixElement?:v {{ Option.default `NORMAL v }}
Suffix <- SuffixElement?:v {{ Option.default `NORMAL v }}
Primary = deco(Primary_aux)
Primary_aux <-
/ Opa_parser.opa_in_braces:e {{ Trx_ast.Code e }}
/ Identifier:e {{ Trx_ast.Rule (snd e) }}
/ OPEN Expression:exp CLOSE {{ Trx_ast.Parens exp }}
/ Literal:lit TILDE?:cs {{ Trx_ast.Literal(lit, cs=None) }}
/ String_expr.string_with_opa:e {{ Trx_ast.DynamicLiteral e }}
/ Class:cls {{ Trx_ast.Class cls }}
/ DOT {{ Trx_ast.Any }}
Class <- '[' (! ']' Range:r {{ r }})*:cls ']' {{ convert_range cls }}
Range <- Char:c1 '-' Char:c2 {{ `RANGE(c1, c2) }}
/ Char:c {{ `ONE(c) }}
Char <- '\\' [nrt'"\\\[\]\-{}] {{ match __2 with 'n' -> '\n' | 'r' -> '\r' | 't' -> '\t' | x -> x }}
/ '\\' [0-9]+ {{ char_of_int (Tgrammar.int_of_chars __2) }}
/ !'\\' .
(**
{6 Lexical analysis}
*)
Identifier <-
/ deco_ml_identifier:n ([.] deco_ml_identifier:s{{s}})+:l {{ List.last (n :: l), dots2 (n :: l) }}
/ deco_ml_identifier:n !EQUAL {{ n, unc2 ident n }}
(* forbid braces [{...}] in literals, if a literal contains braces then we want to interpret
it as an embedded OPA expression. Literal braces need escaping. *)
Literal <- ['] (!['{}] Char {{ __2 }})* ['] {{ Tgrammar.string_of_chars __2 }}
/ ["] (!["{}] Char {{ __2 }})* ["] {{ Tgrammar.string_of_chars __2 }}
AND <- spacing [&]
NOT <- spacing [!]
QUESTION <- spacing [?]
STAR <- spacing [*]
PLUS <- spacing [+]
OPEN <- [(] (* no spacing to avoid f(x) being parsed (which would mean f (~x -> x) which is not usually what the user meant) *)
CLOSE <- spacing [)]
DOT <- spacing [.]
TILDE <- [~] (* no spacing before this one not to mix "mlk"~ t and "mlk" ~f *)
EQUAL <- spacing [=]
COMMA <- spacing [,]
SEMI <- spacing [;]
Jump to Line
Something went wrong with that request. Please try again.