home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fns101.zip / Add-Ons / Fnorb / src / lexer.l < prev    next >
Text File  |  1999-06-28  |  8KB  |  345 lines

  1. %{
  2.  
  3. /***************************************************************
  4.  
  5.   Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  6.   Unpublished work.  All Rights Reserved.
  7.  
  8.   The software contained on this media is the property of the
  9.   DSTC Pty Ltd.  Use of this software is strictly in accordance
  10.   with the license agreement in the accompanying LICENSE.HTML
  11.   file.  If your distribution of this software does not contain
  12.   a LICENSE.HTML file then you have no rights to use this
  13.   software in any manner and should contact DSTC at the address
  14.   below to determine an appropriate licensing arrangement.
  15.  
  16.      DSTC Pty Ltd
  17.      Level 7, GP South
  18.      Staff House Road
  19.      University of Queensland
  20.      St Lucia, 4072
  21.      Australia
  22.      Tel: +61 7 3365 4310
  23.      Fax: +61 7 3365 4311
  24.      Email: enquiries@dstc.edu.au
  25.  
  26.   This software is being provided "AS IS" without warranty of
  27.   any kind.  In no event shall DSTC Pty Ltd be liable for
  28.   damage of any kind arising out of or in connection with
  29.   the use or performance of this software.
  30.  
  31.  
  32.   Project:      Fnorb
  33.   File:         $Source: /units/arch/src/Fnorb/src/RCS/lexer.l,v $
  34.   Version:      @(#)$RCSfile: lexer.l,v $ $Revision: 1.7 $
  35.  
  36.  
  37.   Description:    Flex lexer for OMG IDL.
  38.  
  39. ****************************************************************/
  40.  
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include "Python.h"
  44.  
  45. /* The lexer returns all semantic values as Python objects! */
  46. #define YYSTYPE PyObject*
  47.  
  48. /* Convenience macro to return 'yytext' as a Python string object. */
  49. #define PY_YYTEXT PyString_FromString(yytext)
  50.  
  51. #include "grammar.h"
  52. #include "bisonmodule.h"
  53.  
  54. /* Function defined in 'grammar.y'. */
  55. void yywarning(char*);
  56.  
  57. /* Forward declarations. */
  58. /*PyObject* integer_literal(const char*, const int);*/
  59. PyObject* floating_point_literal(const char*);
  60.  
  61. %}
  62.  
  63. %option noyywrap
  64. %option yylineno
  65.  
  66. WHITESPACE    ([ \t\v\n\f])
  67. SPACE           ([ \t])
  68.  
  69. LETTER        ([a-zA-Z])
  70. DIGIT        ([0-9])
  71. ALPHA        ([a-zA-Z0-9_])
  72. IDENT        ({LETTER}{ALPHA}*)
  73.  
  74. HEX             ([a-fA-F0-9])
  75. OCT             ([0-7])
  76. EXPONENT        ([Ee][+-]?{DIGIT}+)
  77. FS              ([lLfF])
  78. IS              ([lLuU])
  79. FX              ([dD])
  80.  
  81. %% 
  82.  
  83. "#"{SPACE}*"ident".*\n {
  84.     /*
  85.      * '#ident' directive.
  86.      */
  87. }
  88.  
  89. "#"{SPACE}*"pragma".*\n    {
  90.     /*
  91.      * '#pragma' directive.
  92.      *
  93.      * e.g. "# pragma prefix "dstc.edu.au"
  94.      */
  95.     yylval = PY_YYTEXT;
  96.     return PRAGMA;
  97. }
  98.  
  99. "#"{SPACE}*{DIGIT}+{SPACE}+"$pragma".*\n {
  100.     /*
  101.      * '#pragma' directive.
  102.      *
  103.      * e.g. "# 1 pragma prefix "dstc.edu.au"
  104.      */
  105.     yylval = PY_YYTEXT;
  106.     return PRAGMA;
  107. }
  108.  
  109. "#"{SPACE}*{DIGIT}+{SPACE}*\n {
  110.     /*
  111.      * Line/file directive.
  112.      *
  113.      * e.g. "# 1"
  114.      */
  115.     CALL_1("line_directive", PY_YYTEXT);
  116. }
  117.  
  118. "#"{SPACE}*{DIGIT}+{SPACE}+"\""[^\"]*"\"".*\n {
  119.     /*
  120.      * Line/file directive.
  121.      *
  122.      * e.g. "# 1 filename.idl"
  123.      */
  124.     CALL_1("line_directive", PY_YYTEXT);
  125. }
  126.  
  127. "#"{SPACE}*"line"{SPACE}+{DIGIT}+{SPACE}*\n {
  128.     /*
  129.      * '#line' directive.
  130.      *
  131.      * e.g. "# line 1"
  132.      */
  133.     CALL_1("line_directive", PY_YYTEXT);
  134. }
  135.  
  136. "#"{SPACE}*"line"{SPACE}+{DIGIT}+{SPACE}+"\""[^\"]*"\"".*\n {
  137.     /*
  138.      * '#line' directive.
  139.      *
  140.      * e.g. "# line 1 "filename.idl"
  141.      */
  142.     CALL_1("line_directive", PY_YYTEXT);
  143. }
  144.  
  145. "//".*\n {
  146.     /*
  147.      * C++ style comments.
  148.      */
  149. }
  150.  
  151. "/*" {
  152.     /*
  153.      * C style comments.
  154.      */
  155.     while (1)
  156.     {
  157.     int c = input();
  158.     if (c == '*')
  159.     {
  160.         int next = input();
  161.         if (next == '/')
  162.         {
  163.         break;
  164.         }
  165.         else
  166.         {
  167.         unput(next);
  168.         }
  169.     }
  170.     else if (c == EOF)
  171.     {
  172.         yywarning("End of file in comment");
  173.         break;
  174.     }
  175.     }
  176. }
  177.  
  178. any                     return IDL_ANY;
  179. attribute               return ATTRIBUTE;
  180. boolean                 return BOOLEAN;
  181. case                    return CASE;
  182. char                    return CHAR;
  183. const                   return CONST;
  184. context                 return CONTEXT;
  185. default                 return DEFAULT;
  186. double                  return DOUBLE;
  187. enum                    return ENUM;
  188. exception               return EXCEPTION;
  189. fixed                   return FIXED;
  190. float                   return FLOAT;
  191. in                      return IN;
  192. inout                   return INOUT;
  193. interface               return INTERFACE;
  194. long                    return LONG;
  195. module                return MODULE;
  196. native                  return NATIVE;
  197. Object                  return OBJECT;
  198. octet                   return OCTET;
  199. oneway                  return ONEWAY;
  200. out                     return OUT;
  201. raises                  return RAISES;
  202. readonly                return READONLY;
  203. sequence                return SEQUENCE;
  204. short                   return SHORT;
  205. string                  return STRING;
  206. struct            return STRUCT;
  207. switch                  return SWITCH;
  208. typedef                 return TYPEDEF;
  209. union                   return UNION;
  210. unsigned                return UNSIGNED;
  211. void                    return VOID;
  212. wchar                   return WCHAR;
  213. wstring                 return WSTRING;
  214.  
  215. FALSE            return IDL_FALSE;
  216. TRUE            return IDL_TRUE;
  217.  
  218. "<<"                    return LEFT_SHIFT;
  219. ">>"                    return RIGHT_SHIFT;
  220. ::                      return SCOPE_DELIMITER;
  221.  
  222. {IDENT}                 {
  223.                             yylval = PY_YYTEXT;
  224.                     return IDENTIFIER;
  225.                 }
  226.  
  227. {DIGIT}+{IS}?        {
  228.     /*                           
  229.                            yylval = integer_literal(yytext, 10);
  230.     */
  231.                            yylval = PY_YYTEXT;
  232.                            return INTEGER_LITERAL;
  233.             }
  234.  
  235. 0[xX]{HEX}+{IS}?    {
  236.     /*
  237.                             yylval = integer_literal(yytext + 2, 16);
  238.     */
  239.                             yylval = PY_YYTEXT;
  240.                             return INTEGER_LITERAL;
  241.                         }
  242.  
  243. 0{OCT}+{IS}?        {
  244.     /*
  245.                             yylval = integer_literal(yytext + 1, 8);
  246.     */
  247.                             yylval = PY_YYTEXT;
  248.                             return INTEGER_LITERAL;
  249.             }
  250.  
  251. "'"\\({OCT}{1,3})"'"    {
  252.                 yylval = PY_YYTEXT;
  253.                             return CHARACTER_LITERAL;
  254.             }
  255.  
  256. "'"\\x({HEX}{1,2})"'"    {
  257.                 yylval = PY_YYTEXT;
  258.                             return CHARACTER_LITERAL;
  259.             }
  260.  
  261. "'"\\."'"        {
  262.                 yylval = PY_YYTEXT;
  263.                             return CHARACTER_LITERAL;
  264.             }
  265.  
  266. "'"."'"            {
  267.                             yylval = PY_YYTEXT;
  268.                             return CHARACTER_LITERAL;
  269.             }
  270.  
  271. {DIGIT}+{EXPONENT}{FS}?    {
  272.                             yylval = floating_point_literal(yytext);
  273.                             return FLOATING_PT_LITERAL;
  274.             }
  275.  
  276. {DIGIT}*"."{DIGIT}+{EXPONENT}?{FS}? {
  277.                             yylval = floating_point_literal(yytext);
  278.                             return FLOATING_PT_LITERAL;
  279.             }
  280.  
  281. {DIGIT}+"."{DIGIT}*{EXPONENT}?{FS}? {
  282.                             yylval = floating_point_literal(yytext);
  283.                             return FLOATING_PT_LITERAL;
  284.             }
  285.  
  286. {DIGIT}+"."{DIGIT}*{FX} {
  287.                             yylval = PY_YYTEXT;
  288.                             return FIXED_PT_LITERAL;
  289.             }
  290.  
  291. {DIGIT}*"."{DIGIT}+{FX} {
  292.                             yylval = PY_YYTEXT;
  293.                             return FIXED_PT_LITERAL;
  294.             }
  295.  
  296. {DIGIT}+{FX}            {
  297.                             yylval = PY_YYTEXT;
  298.                             return FIXED_PT_LITERAL;
  299.             }
  300.  
  301. "\""([^\"]|"\\\"")*"\""    {
  302.                 yylval = PY_YYTEXT;
  303.                         return STRING_LITERAL;
  304.             }
  305.  
  306. {WHITESPACE}        {
  307.                     /* Ignore whitespace. */
  308.                         }
  309.  
  310. .            {
  311.                             /* Single character tokens (e.g. '{', ';' etc) */
  312.                             return yytext[0];
  313.                 }
  314.  
  315. %%
  316.  
  317. /*
  318. PyObject*
  319. integer_literal(const char* str, const int base)
  320. {
  321.    long value = strtoul(str, NULL, base);
  322.    if(errno == ERANGE)
  323.     {
  324.     yywarning("Integer literal out of range");
  325.     }
  326.  
  327.     return PyInt_FromLong(value);
  328. }
  329. */
  330.  
  331. PyObject*
  332. floating_point_literal(const char* str)
  333. {
  334.     double value = strtod(str, NULL);
  335.     if(errno == ERANGE)
  336.     {
  337.     yywarning("Floating point literal out of range");
  338.     }
  339.  
  340.     return PyFloat_FromDouble(value);
  341. }
  342.  
  343. /***************************************************************/
  344. /* end of lexer.l */
  345.