Initial revision
[ardour.git] / libs / cassowary / ClReader.l
1 /* $Id$
2  Cassowary Incremental Constraint Solver
3  Original Smalltalk Implementation by Alan Borning
4  This C++ Implementation by Greg J. Badros, <gjb@cs.washington.edu>
5  http://www.cs.washington.edu/homes/gjb
6  (C) 1998, 1999 Greg J. Badros and Alan Borning
7  See ../LICENSE for legal details regarding this software
8
9  ClReader.l - Scanner for constraint parsing.
10  By Greg J. Badros
11  */
12
13 %{
14 /* Get the token numbers that bison created for us
15    (uses the -d option of bison) */
16
17 #include <cassowary/ClReader.h>
18 #include "ClReader.cc.h"
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #define CONFIG_H_INCLUDED
23 #endif
24
25 /* global variable for the istream we are reading from;
26    gets set by PcnParseConstraint */
27 istream *pxi_lexer;
28
29 /* Pass in an extra variable (ClParseData *) to cllex so that
30    it can look up variable names */
31 #define YY_DECL int cllex(YYSTYPE *lvalp, void *YYLEX_PARAM)
32
33 /* Make lexer reader from the global variable */
34 #define YY_INPUT(buf,result,max_size) \
35          do { if (pxi_lexer->get(buf[0]) && buf[0] > 0) result = 1; \
36                   else result = YY_NULL; } while (0)
37
38 %}
39
40 %option noyywrap
41
42 DIGIT [0-9]
43 ALPHA [A-Za-z]
44 ALPHANUM [A-Za-z0-9]
45 ID_OK_PUNC [-_\[\]]
46 RO_ANNOTATION "?"
47 ID {ALPHA}({ALPHANUM}|{ID_OK_PUNC})*({RO_ANNOTATION})?
48 NUMID "{"{DIGIT}+"}"
49 ws [ \t\n]+
50
51 %%
52 {ws}                    /* skip whitespace */
53 \n|";"                  { return 0; }
54 ">="                    { return GEQ; }
55 ">"                     { return GT; }
56 "<="                    { return LEQ; }
57 "<"                     { return LT; }
58 "=="                    { return '='; }
59 "="|"-"|"+"|"*"|"/"|"("|")"     { return yytext[0]; }
60
61 {DIGIT}+("."{DIGIT}*)? |
62 "."{DIGIT}+             { lvalp->num = strtod(yytext,0); return NUM; }
63
64 {ID}                    {       /* Lookup the variable name */
65       ClParseData *pclpd = ((ClParseData *) YYLEX_PARAM);
66       int cch = strlen(yytext);
67       ClVariable *pclv = NULL;
68       bool fReadOnly = false;
69       if (yytext[cch-1] == '?') {
70         yytext[cch-1] = '\0';
71         fReadOnly = true;
72       }
73       const string str = string(yytext);
74       pclv = pclpd->_lookup_func(str);
75       if (!pclv->IsNil()) {
76         lvalp->pclv = pclv;
77         return fReadOnly?RO_VAR:VAR;
78       } else {
79         pxi_lexer = NULL;
80         yy_flush_buffer(YY_CURRENT_BUFFER);
81         throw ExCLParseErrorBadIdentifier(str);
82         return 0;
83       }
84    }
85
86 .     { pxi_lexer = NULL; throw ExCLParseErrorMisc("Unrecognized character"); }
87