summaryrefslogtreecommitdiff
path: root/libs/cassowary/ClReader.l
blob: 77fa13a5a1eade002abb407f7365e24e4c4362df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* $Id$
 Cassowary Incremental Constraint Solver
 Original Smalltalk Implementation by Alan Borning
 This C++ Implementation by Greg J. Badros, <gjb@cs.washington.edu>
 http://www.cs.washington.edu/homes/gjb
 (C) 1998, 1999 Greg J. Badros and Alan Borning
 See ../LICENSE for legal details regarding this software

 ClReader.l - Scanner for constraint parsing.
 By Greg J. Badros
 */

%{
/* Get the token numbers that bison created for us
   (uses the -d option of bison) */

#include <cassowary/ClReader.h>
#include "ClReader.cc.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#define CONFIG_H_INCLUDED
#endif

/* global variable for the istream we are reading from;
   gets set by PcnParseConstraint */
istream *pxi_lexer;

/* Pass in an extra variable (ClParseData *) to cllex so that
   it can look up variable names */
#define YY_DECL int cllex(YYSTYPE *lvalp, void *YYLEX_PARAM)

/* Make lexer reader from the global variable */
#define YY_INPUT(buf,result,max_size) \
	 do { if (pxi_lexer->get(buf[0]) && buf[0] > 0) result = 1; \
		  else result = YY_NULL; } while (0)

%}

%option noyywrap

DIGIT [0-9]
ALPHA [A-Za-z]
ALPHANUM [A-Za-z0-9]
ID_OK_PUNC [-_\[\]]
RO_ANNOTATION "?"
ID {ALPHA}({ALPHANUM}|{ID_OK_PUNC})*({RO_ANNOTATION})?
NUMID "{"{DIGIT}+"}"
ws [ \t\n]+

%%
{ws}			/* skip whitespace */
\n|";"			{ return 0; }
">="			{ return GEQ; }
">"			{ return GT; }
"<=" 			{ return LEQ; }
"<" 			{ return LT; }
"==" 			{ return '='; }
"="|"-"|"+"|"*"|"/"|"("|")" 	{ return yytext[0]; }

{DIGIT}+("."{DIGIT}*)? |
"."{DIGIT}+		{ lvalp->num = strtod(yytext,0); return NUM; }

{ID} 			{       /* Lookup the variable name */
      ClParseData *pclpd = ((ClParseData *) YYLEX_PARAM);
      int cch = strlen(yytext);
      ClVariable *pclv = NULL;
      bool fReadOnly = false;
      if (yytext[cch-1] == '?') {
	yytext[cch-1] = '\0';
	fReadOnly = true;
      }
      const string str = string(yytext);
      pclv = pclpd->_lookup_func(str);
      if (!pclv->IsNil()) {
        lvalp->pclv = pclv;
        return fReadOnly?RO_VAR:VAR;
      } else {
	pxi_lexer = NULL;
	yy_flush_buffer(YY_CURRENT_BUFFER);
	throw ExCLParseErrorBadIdentifier(str);
        return 0;
      }
   }

.     {	pxi_lexer = NULL; throw ExCLParseErrorMisc("Unrecognized character"); }