Skip to content
This repository
tree: be384c554a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 258 lines (212 sloc) 9.021 kb
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
(*
    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 Tgrammar
  module PG = Tgrammar.PreGrammar

  let current_location fn pos =
    { file_name = fn; line_number = fst (FilePos.get_pos fn pos) }

}}

extra _grammar : {string Tgrammar.PreGrammar.pre_grammar}
extra _file_name : {string}
extra _module_name : {string}
extra _stoppable : {bool}

/** TRX grammar **/
+Grammar : {string Tgrammar.PreGrammar.pre_grammar} <- Spacing GElems EOF {{ __2 }}

/** list of TRX elements **/
GElems <- IncludeRead GElems {{ let key, id = __1 in { __2 with PG.incl = StringMap.add key id __2.PG.incl } }}
/ Extra GElems {{ { __2 with PG.pextra = __1 :: __2.PG.pextra } }}
/ Option GElems {{ { __2 with PG.poptions = __1 :: __2.PG.poptions } }}
/ HeaderCode GElems {{ { __2 with PG.pheader = __1 :: __2.PG.pheader } }}
/ Function GElems {{ add_function __2 __1 }}
/ Definition GElems {{ add_definition __2 __1 }}
/ Spacing {{ _grammar }}

strict_GElem <- IncludeRead {{ `read __1 }}
/ Extra {{ `extra __1 }}
        / Option {{ `option __1 }}
/ HeaderCode {{ `header __1 }}
/ Function {{ `func __1 }}
/ Definition {{ `def __1 }}

/** TRX element **/
GElem <- strict_GElem {{ __1 }}
       / Spacing {{ `spacing }} ;

GElem_no_sp <- Spacing strict_GElem Spacing {{ __2 }} ;

maybe_GElem_no_sp <- GElem_no_sp {{ `success __1 }}
                   / (!GElem_no_sp .)+ {{ `failure (Base.String.of_chars __1, _pos_beg, _pos_end) }} ;
maybe_GElems_no_sp <- maybe_GElem_no_sp* ;
maybe_GElems_no_sp_eof <- maybe_GElems_no_sp EOF ;

/** TRX rule **/
Definition <- SEMI? rule_annots:annots KEEP_CACHE?:cache DEBUG?:debug MARK?:mark Identifier:id Type?:rtype DefExpr:expr SEMI?
{{ (*jlog ~level:2 (Printf.sprintf "definition: %s" id) ;*)
let entry =
{ PG.expression = expr
; debug = debug <> None
; mark = mark <> None
; retain_cache = cache <> None
; rule_type = rtype
; origin = Some (current_location _file_name _pos_beg)
}
in
(id, (entry, annots))
}}

let construct_name_aux prefix suffix =
  prefix Spacing ((!(Spacing suffix) .)* $_) Spacing suffix Spacing {{ __3 }}

construct_name_primary = construct_name_aux("/**", "**/");
construct_name_secondary = construct_name_aux("/*", "*/" );

/* optional rule name */
construct_name_opt <- construct_name_primary {{ PrimaryName __1 }}
                    / construct_name_secondary {{ SecondaryName __1 }}

/* memoization annotation */
memo_opt <- "{$"
             ( "0" {{ MemoNone }}
             / "F" {{ MemoFail }}
             / "S" {{ MemoSuccess }}
             / "1" {{ MemoFull }}
             )
             "}" Spacing {{ __2 }}

/* rule annotations */
rule_annots <- construct_name_opt? memo_opt?
  {{ let name = match __1 with Some r -> r | None -> NoName in
     let memo = match __2 with Some m -> m | None -> MemoNoInfo in
       { rule_name = name; rule_memo = memo }
  }}

DefExpr <- LEFTARROW Expression {{ PG.Expr __2 }}
/ EQUAL Identifier Exprs {{ PG.App (__2, __3) }}

/* list of expressions */
Exprs <- OPEN Expression (COMMA Expression {{ __2 }})* CLOSE {{ __2 :: __3 }}
       / !OPEN Expression {{ [ __2 ] }}

/** TRX function **/
Function <- rule_annots LET Identifier Identifier+ EQUAL Expression SEMI?
{{ (*jlog (Printf.sprintf "function: %s" __3) ;*)
(__3, ({ PG.vars = __4 ; expr = PG.Expr __6 }, __1)) }}

/** include directive **/
IncludeRead <- ("include" {{ PG.Incl }} / "read" {{ PG.Read }}) Spacing Filename Spacing ("global" Spacing Names {{ __3 }})?
{{ __3, { PG.it = __1 ; gl = match __5 with Some l -> l | _ -> [] } }}

Type <- Spacing ":" Spacing LBRACE ((!RBRACE .)* $_):t RBRACE Spacing {{ t }}

/* extra directive */
Extra <- "extra" Space Spacing mlvar:var Type:vartype {{ var, vartype }}

/* TRX option */
Option <- "%%" optionType Spacing "=" Spacing mlvar Spacing {{ __2, __6 }}

/* element name */
mlvar <- [a-z_][a-zA-Z0-9_]* $_
/* TRX option name */
optionType <- [a-z][a-zA-Z0-9_\-]* $_
/* file name */
Filename <- Literal / (!Space .)+ $_

/** rule body **/
Expression <- SLASH? Sequence (SLASH Sequence {{ __2 }})* {{ __2 :: __3 }}

item_list <- (Prefix Primary Suffix (Spacing [:] [_]? Spacing mlvar Spacing {{ __5, __3 <> None }})? {{ (__1, __2, __3), __4 }})+
{{ List.fold_left_i (
fun (liste,map) (item, option) num ->
match option with
None -> (liste @ [item]), map
| Some (label, b) -> (liste @ [item]), (StringMap.add label ((string_of_int (num+1)),b) map)
) ([], StringMap.empty) __1 }}

/* a sequence of parsing expressions */
Sequence <- item_list Code? {{ let liste, map = __1 in liste, map, __2 }}

BackId <- [`] Identifier $_

HeaderCode <- ( "inside:" {{fun x -> `inside x}}
              / "types:" {{fun x -> `types x}}
/ "decls:" {{fun x -> `decls x}}
/ "" {{fun x -> `normal x}}
              ):variant DefaultCodeNoStop:code
              {{ variant code, Some (current_location _file_name _pos_beg) }}

CodeRange <- (":_" Spacing)? {{ __1 <> None }}
StoppableCode <- ("!!" Spacing)? {{ __1 <> None }}

let code_aux beg end =
  beg Spacing StoppableCode CodeRange ((!end .)* $_) end {{ __4, (if (__3 && _stoppable) or (not _stoppable) then __5 else Printf.sprintf "Some (%s)" __5) }}

DefaultCode <- (=code_aux(BEGIn, ENd)) {{ let (u, v) = __1 in u, v, Some(current_location _file_name _pos_beg), true }}
             / (=code_aux(BEGIN, END)) {{ let (u, v) = __1 in u, v, Some(current_location _file_name _pos_beg), false }}
DefaultCodeNoStop <- BEGIN ((!END .)* $_) END {{ __2 }}

/* rule production */
Code <- DefaultCode
/ "$_" CodeRange Spacing {{ __2, ".sub", None, false }}
/ "$:" ([0-9]+ $_) Spacing {{ true, Printf.sprintf "%s__%s" (if _stoppable then "Some " else "") __2, None, false }}
/ DOLLAR {{ false, Printf.sprintf "%s()" (if _stoppable then "Some " else ""), None, false }}
PrefixElement <- AND {{ `AND }} / NOT {{ `NOT }}
SuffixElement <- QUESTION {{`QUESTION }} / STAR {{`STAR}} / PLUS {{`PLUS}}
/* element's prefix */
Prefix <- PrefixElement? {{ match __1 with None -> `NORMAL | Some x -> x }}
/* element's suffix */
Suffix <- SuffixElement? {{ match __1 with None -> `NORMAL | Some x -> x }}
/* parsing element */
Primary <- Identifier !LEFTARROW {{ PG.Ident __1 }}
/ OPEN EQUAL Identifier Exprs CLOSE {{ PG.Paren (PG.App (__3, __4)) }}
/ OPEN Expression CLOSE {{ PG.Paren (PG.Expr __2) }}
/ Literal TILDE? {{ PG.Literal (__1, Option.is_none __2) }}
/ Class {{ let range, negation = __1 in
let _class = PG.Class range in
if negation then
PG.Paren (PG.Expr [[`NOT, _class, `NORMAL ; `NORMAL, PG.Class [Any], `NORMAL], StringMap.empty, None])
else
_class
}}
/ DOT {{ PG.Class [Any] }}

# Lexical syntax

Module <- [A-Z][A-Za-z0-9_]* $_
Name <- [a-zA-Z_] [a-zA-Z0-9_]* $_
Names <- OPEN (Name Spacing COMMA? {{ __1 }})* CLOSE {{ __2 }}

/* identifier */
Identifier <- (
/ Module [.] Name {{ __1 ^ "_" ^ __3 }}
/ Name {{ _module_name ^ "_" ^ __1 }}
) Spacing {{ __1 }}
/* literal */
Literal <- ['] (!['] Char {{ __2 }})* ['] Spacing {{ string_of_chars __2 }}
/ [\"] (![\"] Char {{ __2 }})* [\"] Spacing {{ string_of_chars __2 }}
/* class of symbols */
Class <- '[' '^'? (!']' Range {{ __2 }})* ']' Spacing {{ __3, (Option.is_some __2) }}
   

Range <- Char '-' Char {{ Range (__1, __3) }}
       / Char {{ One __1 }}
Char <- '\\' [nrt'\"\\\[\]\-] {{ match __2 with 'n' -> '\n' | 'r' -> '\r' | 't' -> '\t' | x -> x }}
      / '\\' [0-9]+ {{ char_of_int (int_of_chars __2) }}
      / !'\\' .

MARK <- [+]
KEEP_CACHE <- "<icache>" Spacing
DEBUG <- [%]
LBRACE <- '{' Spacing
RBRACE <- '}' Spacing
BEGIN <- '{{'
END <- '}}' Spacing
BEGIn <- '{|'
ENd <- '|}' Spacing
LEFTARROW <- '<-' Spacing
RIGHTARROW <- '->' Spacing
SLASH <- [/] Spacing
AND <- [&] Spacing
NOT <- [!] Spacing
QUESTION <- [?] Spacing
STAR <- [*] Spacing
PLUS <- [+] Spacing
OPEN <- [(] Spacing
CLOSE <- [)] Spacing
DOT <- [.] Spacing
DOLLAR <- [$] Spacing
TILDE <- [~] Spacing
EQUAL <- [=] Spacing
COMMA <- [,] Spacing
SEMI <- [;] Spacing
COLON <- [:] Spacing

LET <- "let" Spacing

/* spacing */
Spacing <- (Space $/ Comment)*
/* a comment */
Comment <- '#' (!EOL .)* (EOL / EOF) $
/ mlcomment
mlcomment <- mlCOMMENTSTART (!mlCOMMENTSTOP (mlcomment / . $))* mlCOMMENTSTOP $
mlCOMMENTSTART <- '(*' Spacing
mlCOMMENTSTOP <- '*)' Spacing

Space <- [ \t] $ / EOL $
/* end of line */
EOL <- [\n\r] $ / '\r' '\n' $
EOF <- !.
Something went wrong with that request. Please try again.