Commit 5b199ff7 authored by POTTIER Francois's avatar POTTIER Francois

Lexer: emit the tokens ATTRIBUTE, GRAMMARATTRIBUTE, PERCENTATTRIBUTE.

parent 54fbd462
...@@ -25,6 +25,8 @@ open Positions ...@@ -25,6 +25,8 @@ open Positions
%token <Stretch.ocamltype> OCAMLTYPE %token <Stretch.ocamltype> OCAMLTYPE
%token <Stretch.t Lazy.t> PERCENTPERCENT %token <Stretch.t Lazy.t> PERCENTPERCENT
%token <Syntax.identifier option array -> Action.t> ACTION %token <Syntax.identifier option array -> Action.t> ACTION
%token <Syntax.attribute> ATTRIBUTE GRAMMARATTRIBUTE
%token PERCENTATTRIBUTE
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* Start symbol. */ /* Start symbol. */
......
...@@ -333,6 +333,8 @@ let uppercase = ['A'-'Z' '\192'-'\214' '\216'-'\222'] ...@@ -333,6 +333,8 @@ let uppercase = ['A'-'Z' '\192'-'\214' '\216'-'\222']
let identchar = ['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '0'-'9'] (* '\'' forbidden *) let identchar = ['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '0'-'9'] (* '\'' forbidden *)
let attributechar = identchar | '.'
let poskeyword = let poskeyword =
'$' '$'
(("symbolstart" | "start" | "end") as where) (("symbolstart" | "start" | "end") as where)
...@@ -370,6 +372,8 @@ rule main = parse ...@@ -370,6 +372,8 @@ rule main = parse
{ PARAMETER } { PARAMETER }
| "%inline" | "%inline"
{ INLINE } { INLINE }
| "%attribute"
{ PERCENTATTRIBUTE }
| "%on_error_reduce" | "%on_error_reduce"
{ ON_ERROR_REDUCE } { ON_ERROR_REDUCE }
| "%%" | "%%"
...@@ -441,6 +445,20 @@ rule main = parse ...@@ -441,6 +445,20 @@ rule main = parse
Action.from_stretch stretch Action.from_stretch stretch
) )
) } ) }
| ('%'? as percent) "[@" (attributechar+ as id) whitespace*
{ let openingpos = lexeme_start_p lexbuf in
let stretchpos = lexeme_end_p lexbuf in
let closingpos = attribute openingpos lexbuf in
let pos = Positions.lex_join openingpos closingpos in
let attr = mk_stretch stretchpos closingpos false [] in
Printf.fprintf stderr "Coucou\n%!";
if percent = "" then
(* No [%] sign: this is a normal attribute. *)
ATTRIBUTE (Positions.with_pos pos id, attr)
else
(* A [%] sign is present: this is a grammar-wide attribute. *)
GRAMMARATTRIBUTE (Positions.with_pos pos id, attr)
}
| eof | eof
{ EOF } { EOF }
| _ | _
...@@ -575,6 +593,40 @@ and parentheses openingpos monsters = parse ...@@ -575,6 +593,40 @@ and parentheses openingpos monsters = parse
(* ------------------------------------------------------------------------ *) (* ------------------------------------------------------------------------ *)
(* Collect an attribute payload, which is terminated by a closing square
bracket. Nested square brackets must be properly counted. Nested curly
brackets and nested parentheses are also kept track of, so as to better
report errors when they are not balanced. *)
and attribute openingpos = parse
| '['
{ let _ = attribute (lexeme_start_p lexbuf) lexbuf in
attribute openingpos lexbuf }
| ']'
{ lexeme_start_p lexbuf }
| '{'
{ let _, _ = action false (lexeme_start_p lexbuf) [] lexbuf in
attribute openingpos lexbuf }
| '('
{ let _, _ = parentheses (lexeme_start_p lexbuf) [] lexbuf in
attribute openingpos lexbuf }
| '"'
{ string (lexeme_start_p lexbuf) lexbuf; attribute openingpos lexbuf }
| "'"
{ char lexbuf; attribute openingpos lexbuf }
| "(*"
{ ocamlcomment (lexeme_start_p lexbuf) lexbuf; attribute openingpos lexbuf }
| newline
{ new_line lexbuf; attribute openingpos lexbuf }
| '}'
| ')'
| eof
{ error1 openingpos "unbalanced opening bracket." }
| _
{ attribute openingpos lexbuf }
(* ------------------------------------------------------------------------ *)
(* Skip O'Caml comments. Comments can be nested and can contain (* Skip O'Caml comments. Comments can be nested and can contain
strings or characters, which must be correctly analyzed. (A string strings or characters, which must be correctly analyzed. (A string
could contain begin-of-comment or end-of-comment sequences, which could contain begin-of-comment or end-of-comment sequences, which
......
...@@ -43,6 +43,17 @@ type action = ...@@ -43,6 +43,17 @@ type action =
(* ------------------------------------------------------------------------ *) (* ------------------------------------------------------------------------ *)
(* An attribute consists of an attribute name and an attribute payload. The
payload is an uninterpreted stretch of source text. *)
type attribute =
string Positions.located * Stretch.t
type attributes =
attribute list
(* ------------------------------------------------------------------------ *)
(* Information about tokens. (Only after joining.) *) (* Information about tokens. (Only after joining.) *)
type token_associativity = type token_associativity =
......
...@@ -19,6 +19,8 @@ open Positions ...@@ -19,6 +19,8 @@ open Positions
%token <Stretch.ocamltype> OCAMLTYPE %token <Stretch.ocamltype> OCAMLTYPE
%token <Stretch.t Lazy.t> PERCENTPERCENT %token <Stretch.t Lazy.t> PERCENTPERCENT
%token <Syntax.identifier option array -> Syntax.action> ACTION %token <Syntax.identifier option array -> Syntax.action> ACTION
%token <Syntax.attribute> ATTRIBUTE GRAMMARATTRIBUTE
%token PERCENTATTRIBUTE
%start grammar %start grammar
%type <Syntax.partial_grammar> grammar %type <Syntax.partial_grammar> grammar
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment