Implement -include (optional include) command.
[goals.git] / src / lexer.mll
1 (* Goalfile lexer
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
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.
9  *
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.
14  *
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.
18  *)
19
20 {
21 open Lexing
22 open Parser
23
24 open Printf
25
26 exception SyntaxError of string
27
28 let new_line lexbuf =
29   let pos = lexbuf.lex_curr_p in
30   lexbuf.lex_curr_p <-
31     { pos with pos_bol = lexbuf.lex_curr_pos; pos_lnum = pos.pos_lnum + 1 }
32 }
33
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' '_']*
38
39 rule read =
40     parse
41     | white
42     | comment { read lexbuf }
43     | newline { new_line lexbuf; read lexbuf }
44     | ","     { COMMA }
45     | ":"     { COLON }
46     | "="     { EQUALS }
47     | "("     { LEFT_PAREN }
48     | ")"     { RIGHT_PAREN }
49     | "["     { LEFT_ARRAY }
50     | "]"     { RIGHT_ARRAY }
51     | '"'     { read_string (Ast.Substs.create ()) lexbuf }
52     | "{"     { read_code (Ast.Substs.create ()) (ref 1) lexbuf }
53     | "goal"  { GOAL }
54     | "tactic" { TACTIC_KEYWORD }
55     | "let"   { LET }
56     | "include" { INCLUDE }
57     | "-include" { OPTINCLUDE }
58     | "*" id  { (* NB: The initial '*' is part of the name. *)
59                 TACTIC (Lexing.lexeme lexbuf) }
60     | id      { ID (Lexing.lexeme lexbuf) }
61     | _       { raise (SyntaxError ("unexpected character: " ^
62                                     Lexing.lexeme lexbuf)) }
63     | eof     { EOF }
64
65 (* Parse "STRING" literal with %-substitutions. *)
66 and read_string buf =
67     parse
68     | '\\' '"'
69               { Ast.Substs.add_char buf '"'; read_string buf lexbuf }
70     | '\\' 'a'
71               { Ast.Substs.add_char buf '\007'; read_string buf lexbuf }
72     | '\\' 'b'
73               { Ast.Substs.add_char buf '\008'; read_string buf lexbuf }
74     | '\\' 't'
75               { Ast.Substs.add_char buf '\009'; read_string buf lexbuf }
76     | '\\' 'n'
77               { Ast.Substs.add_char buf '\010'; read_string buf lexbuf }
78     | '\\' 'v'
79               { Ast.Substs.add_char buf '\011'; read_string buf lexbuf }
80     | '\\' 'f'
81               { Ast.Substs.add_char buf '\012'; read_string buf lexbuf }
82     | '\\' 'r'
83               { Ast.Substs.add_char buf '\013'; read_string buf lexbuf }
84     | '\\' '\\'
85               { Ast.Substs.add_char buf '\\'; read_string buf lexbuf }
86     | '"'     { STRING (Ast.Substs.get buf) }
87     | newline { Ast.Substs.add_char buf '\n';
88                 new_line lexbuf; read_string buf lexbuf }
89     | '%' '%' { Ast.Substs.add_char buf '%'; read_string buf lexbuf }
90     | '%' id  { let id = Lexing.lexeme lexbuf in
91                 let len = String.length id in
92                 Ast.Substs.add_var buf (String.sub id 1 (len-1));
93                 read_string buf lexbuf }
94     | '%' _   { raise (SyntaxError ("illegal character in %-substitution: " ^
95                                     Lexing.lexeme lexbuf)) }
96     | [^ '"' '\\' '\r' '\n' '%' ]+
97               { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
98                 read_string buf lexbuf }
99     | _       { raise (SyntaxError ("illegal character in string: " ^
100                                       Lexing.lexeme lexbuf)) }
101     | eof     { raise (SyntaxError ("unterminated string")) }
102
103 (* Parse { CODE } literal with %-substitutions.
104  *
105  * Note the range of %-substitutions possible is larger than
106  * for strings.
107  *)
108 and read_code buf level =
109     parse
110     | '{'     { incr level; read_code buf level lexbuf }
111     | '}'     { decr level;
112                 if !level = 0 then CODE (Ast.Substs.get buf)
113                 else (
114                   Ast.Substs.add_char buf '}';
115                   read_code buf level lexbuf
116                 ) }
117     | newline { Ast.Substs.add_char buf '\n';
118                 new_line lexbuf; read_code buf level lexbuf }
119     | '%' '%' { Ast.Substs.add_char buf '%'; read_code buf level lexbuf }
120     | '%' '@' { Ast.Substs.add_var buf "@"; read_code buf level lexbuf }
121     | '%' '<' { Ast.Substs.add_var buf "<"; read_code buf level lexbuf }
122     | '%' '^' { Ast.Substs.add_var buf "^"; read_code buf level lexbuf }
123     | '%' id  { let id = Lexing.lexeme lexbuf in
124                 let len = String.length id in
125                 Ast.Substs.add_var buf (String.sub id 1 (len-1));
126                 read_code buf level lexbuf }
127     | '%' _   { raise (SyntaxError ("illegal character in %-substitution: " ^
128                                       Lexing.lexeme lexbuf)) }
129     | [^ '{' '}' '\r' '\n' '%' ]+
130               { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
131                 read_code buf level lexbuf }
132     | _       { raise (SyntaxError ("illegal character in code section: " ^
133                                       Lexing.lexeme lexbuf)) }
134     | eof     { raise (SyntaxError ("unterminated code section")) }