stdlib: Implement wildcard function.
[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     | ";"     { SEMICOLON }
47     | "="     { EQUALS }
48     | "("     { LEFT_PAREN }
49     | ")"     { RIGHT_PAREN }
50     | "["     { LEFT_ARRAY }
51     | "]"     { RIGHT_ARRAY }
52     | '"'     { read_string (Ast.Substs.create ()) lexbuf }
53     | "{"     { read_code (Ast.Substs.create ()) (ref 1) lexbuf }
54     | "goal"  { GOAL }
55     | "tactic"
56               { TACTIC_KEYWORD }
57     | "function"
58               { FUNCTION }
59     | "let"   { LET }
60     | "include"
61               { INCLUDE }
62     | "-include"
63               { OPTINCLUDE }
64     | "*" id  { (* NB: The initial '*' is part of the name. *)
65                 TACTIC (Lexing.lexeme lexbuf) }
66     | id      { ID (Lexing.lexeme lexbuf) }
67     | _       { raise (SyntaxError ("unexpected character: " ^
68                                     Lexing.lexeme lexbuf)) }
69     | eof     { EOF }
70
71 (* Parse "STRING" literal with %-substitutions. *)
72 and read_string buf =
73     parse
74     | '\\' '"'
75               { Ast.Substs.add_char buf '"'; read_string buf lexbuf }
76     | '\\' 'a'
77               { Ast.Substs.add_char buf '\007'; read_string buf lexbuf }
78     | '\\' 'b'
79               { Ast.Substs.add_char buf '\008'; read_string buf lexbuf }
80     | '\\' 't'
81               { Ast.Substs.add_char buf '\009'; read_string buf lexbuf }
82     | '\\' 'n'
83               { Ast.Substs.add_char buf '\010'; read_string buf lexbuf }
84     | '\\' 'v'
85               { Ast.Substs.add_char buf '\011'; read_string buf lexbuf }
86     | '\\' 'f'
87               { Ast.Substs.add_char buf '\012'; read_string buf lexbuf }
88     | '\\' 'r'
89               { Ast.Substs.add_char buf '\013'; read_string buf lexbuf }
90     | '\\' '\\'
91               { Ast.Substs.add_char buf '\\'; read_string buf lexbuf }
92     | '"'     { STRING (Ast.Substs.get buf) }
93     | newline { Ast.Substs.add_char buf '\n';
94                 new_line lexbuf; read_string buf lexbuf }
95     | '%' '%' { Ast.Substs.add_char buf '%'; read_string buf lexbuf }
96     | '%' id  { let id = Lexing.lexeme lexbuf in
97                 let len = String.length id in
98                 Ast.Substs.add_var buf (String.sub id 1 (len-1));
99                 read_string buf lexbuf }
100     | '%' _   { raise (SyntaxError ("illegal character in %-substitution: " ^
101                                     Lexing.lexeme lexbuf)) }
102     | [^ '"' '\\' '\r' '\n' '%' ]+
103               { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
104                 read_string buf lexbuf }
105     | _       { raise (SyntaxError ("illegal character in string: " ^
106                                       Lexing.lexeme lexbuf)) }
107     | eof     { raise (SyntaxError ("unterminated string")) }
108
109 (* Parse { CODE } literal with %-substitutions.
110  *
111  * Note the range of %-substitutions possible is larger than
112  * for strings.
113  *)
114 and read_code buf level =
115     parse
116     | '{'     { Ast.Substs.add_char buf '{';
117                 incr level; read_code buf level lexbuf }
118     | '}'     { decr level;
119                 if !level = 0 then CODE (Ast.Substs.get buf)
120                 else (
121                   Ast.Substs.add_char buf '}';
122                   read_code buf level lexbuf
123                 ) }
124     | newline { Ast.Substs.add_char buf '\n';
125                 new_line lexbuf; read_code buf level lexbuf }
126     | '%' '%' { Ast.Substs.add_char buf '%'; read_code buf level lexbuf }
127     | '%' '@' { Ast.Substs.add_var buf "@"; read_code buf level lexbuf }
128     | '%' '<' { Ast.Substs.add_var buf "<"; read_code buf level lexbuf }
129     | '%' '^' { Ast.Substs.add_var buf "^"; read_code buf level lexbuf }
130     | '%' id  { let id = Lexing.lexeme lexbuf in
131                 let len = String.length id in
132                 Ast.Substs.add_var buf (String.sub id 1 (len-1));
133                 read_code buf level lexbuf }
134     | '%' _   { raise (SyntaxError ("illegal character in %-substitution: " ^
135                                       Lexing.lexeme lexbuf)) }
136     | [^ '{' '}' '\r' '\n' '%' ]+
137               { Ast.Substs.add_string buf (Lexing.lexeme lexbuf);
138                 read_code buf level lexbuf }
139     | _       { raise (SyntaxError ("illegal character in code section: " ^
140                                       Lexing.lexeme lexbuf)) }
141     | eof     { raise (SyntaxError ("unterminated code section")) }