%{ /* Formula lexer by David A. Wheeler. Released under the "MIT license": Copyright (c) 2005 David A. Wheeler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Notes: + Based on info in sections 8.1.3 and 8.3.1 + Numbers are stored in "C" locale ("." decimal separator, no thousands separator, etc.). Remember handle them like this or equivalent: old_locale = strdup (setlocale (LC_NUMERIC, NULL)); setlocale (LC_NUMERIC, "C"); + Numbers can begin with a leading ".", e.g., ".01" is legal. + Numbers can end in %, which divides that number by 100. (Note that 2+10% is 2.1, not 2.2) + Not handled: bases other than base 10. It might be nice to accept an Ada-like syntax, e.g. base#basednumber# where base is 2..36. Issue: if special codes ALSO begin with '#' that makes patterns slightly more interesting (though still doable). Alternatively, can use C's 0x (hex) and 0 (octal), but what about base 2? + Leading - and + signs are allowed (they're implemented here by grammar) + Constant Strings should be in UTF-8 (no need to encode as &#..., and DO NOT re-encode each byte). + Constant strings surrounded with double quotes. + In strings, to include a ", repeat " twice. Note that in the XML, all double-quotes are escaped as " + The "&" operator concatenates; in the XML, represented as & + Need to handle special values (#ERR, #NAN, #NA, etc.) + This doesn't accept "==" for equal, nor "!=" for "not equal to" A _reader_ should probably accept those (and ** for ^). + Tables can't have ' in their name, but need ' to surround other characters (use ' in the XML). + Need to define error values and special values. SHOULD allow #NA (or #N/A), #NV, #NAN (not a number). SHOULD allow #TRUE and #FALSE (instead of only TRUE() and FALSE()). + cellrangeaddresslist - Whitespace is otherwise ignored outside string constants, so it seems inconsistent to suddenly make whitespace meaningful here. + If identifiers are case-insensitive, should writers be told to write uppercase, lowercase, or doesn't matter? */ #include #include "y.tab.h" // extern void yyerror(char *message); %} simple_number [0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?%? number ((\.[0-9]+)|([0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?))%? string \"([^"]|\"\")*\" identifier [A-Za-z][A-Za-z0-9_]* celladdress ($?([^\. ']+|'[^']+'))?\.$?[A-Z]+$?[0-9]+ cellrangeaddress {celladdress}(\:{celladdress})? cellrangeaddresslist {cellrangeaddress}([ \t]+{cellrangeaddress})* %% {number} return NUMBER; {string} return STRING; {identifier} return IDENTIFIER; {cellrangeaddresslist} return CELLRANGEADDRESSLIST; [-+*/^()<>=.,@:\[\]\&;] return *yytext; /* Return the character */ ">=" return GE; "<=" return LE; "<>" return NE; /* Should != or == also be allowed? */ [ \t\n]+ ; /* ignore whitespace */ . yyerror("Unknown character"); %% int yywrap(void) { return 1; /* Halt processing at end of text */ }