2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 exception SyntaxError of string
29 let pos = lexbuf.lex_curr_p in
31 { pos with pos_bol = lexbuf.lex_curr_pos; pos_lnum = pos.pos_lnum + 1 }
34 let white = [' ' '\t']+
35 let newline = '\r' | '\n' | "\r\n"
36 let comment = '#' (_#'\n')*
37 let id = ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']*
42 | comment { read lexbuf }
43 | newline { new_line lexbuf; read lexbuf }
51 | '"' { read_string (Ast.Substs.create ()) lexbuf }
52 | "{" { read_code (Ast.Substs.create ()) (ref 1) lexbuf }
55 | "*" id { let id = Lexing.lexeme lexbuf in
56 let len = String.length id in
57 TACTIC (String.sub id 1 (len-1)) }
58 | id { ID (Lexing.lexeme lexbuf) }
59 | _ { raise (SyntaxError ("unexpected character: " ^
60 Lexing.lexeme lexbuf)) }
63 (* Parse "STRING" literal with %-substitutions. *)
67 { Ast.Substs.add_char buf '"'; read_string buf lexbuf }
69 { Ast.Substs.add_char buf '\007'; read_string buf lexbuf }
71 { Ast.Substs.add_char buf '\008'; read_string buf lexbuf }
73 { Ast.Substs.add_char buf '\009'; read_string buf lexbuf }
75 { Ast.Substs.add_char buf '\010'; read_string buf lexbuf }
77 { Ast.Substs.add_char buf '\011'; read_string buf lexbuf }
79 { Ast.Substs.add_char buf '\012'; read_string buf lexbuf }
81 { Ast.Substs.add_char buf '\013'; read_string buf lexbuf }
83 { Ast.Substs.add_char buf '\\'; read_string buf lexbuf }
84 | '"' { STRING (Ast.Substs.get buf) }
85 | newline { Ast.Substs.add_char buf '\n';
86 new_line lexbuf; read_string buf lexbuf }
87 | '%' '%' { Ast.Substs.add_char buf '%'; read_string buf lexbuf }
88 | '%' id { let id = Lexing.lexeme lexbuf in
89 let len = String.length id in
90 Ast.Substs.add_var buf (String.sub id 1 (len-1));
91 read_string buf lexbuf }
92 | '%' _ { raise (SyntaxError ("illegal character in %-substitution: " ^
93 Lexing.lexeme lexbuf)) }
94 | [^ '"' '\\' '\r' '\n' '%' ]+
95 { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
96 read_string buf lexbuf }
97 | _ { raise (SyntaxError ("illegal character in string: " ^
98 Lexing.lexeme lexbuf)) }
99 | eof { raise (SyntaxError ("unterminated string")) }
101 (* Parse { CODE } literal with %-substitutions.
103 * Note the range of %-substitutions possible is larger than
106 and read_code buf level =
108 | '{' { incr level; read_code buf level lexbuf }
110 if !level = 0 then CODE (Ast.Substs.get buf)
112 Ast.Substs.add_char buf '}';
113 read_code buf level lexbuf
115 | newline { Ast.Substs.add_char buf '\n';
116 new_line lexbuf; read_code buf level lexbuf }
117 | '%' '%' { Ast.Substs.add_char buf '%'; read_code buf level lexbuf }
118 | '%' '@' { Ast.Substs.add_var buf "@"; read_code buf level lexbuf }
119 | '%' '<' { Ast.Substs.add_var buf "<"; read_code buf level lexbuf }
120 | '%' '^' { Ast.Substs.add_var buf "^"; read_code buf level lexbuf }
121 | '%' id { let id = Lexing.lexeme lexbuf in
122 let len = String.length id in
123 Ast.Substs.add_var buf (String.sub id 1 (len-1));
124 read_code buf level lexbuf }
125 | '%' _ { raise (SyntaxError ("illegal character in %-substitution: " ^
126 Lexing.lexeme lexbuf)) }
127 | [^ '{' '}' '\r' '\n' '%' ]+
128 { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
129 read_code buf level lexbuf }
130 | _ { raise (SyntaxError ("illegal character in code section: " ^
131 Lexing.lexeme lexbuf)) }
132 | eof { raise (SyntaxError ("unterminated code section")) }