Revision: 46329
          http://brlcad.svn.sourceforge.net/brlcad/?rev=46329&view=rev
Author:   n_reed
Date:     2011-08-23 14:07:22 +0000 (Tue, 23 Aug 2011)

Log Message:
-----------
Replaced flex scanner with re2c equivalent.

Modified Paths:
--------------
    brlcad/trunk/src/libgcv/wfobj/obj_grammar.h.in
    brlcad/trunk/src/libgcv/wfobj/obj_grammar.yy
    brlcad/trunk/src/libgcv/wfobj/obj_parser.cpp
    brlcad/trunk/src/libgcv/wfobj/obj_parser.h

Added Paths:
-----------
    brlcad/trunk/src/libgcv/wfobj/obj_rules.h
    brlcad/trunk/src/libgcv/wfobj/obj_rules.re
    brlcad/trunk/src/libgcv/wfobj/obj_token_type.h
    brlcad/trunk/src/libgcv/wfobj/re2c_utils.c
    brlcad/trunk/src/libgcv/wfobj/re2c_utils.h

Removed Paths:
-------------
    brlcad/trunk/src/libgcv/wfobj/obj_rules.ll

Modified: brlcad/trunk/src/libgcv/wfobj/obj_grammar.h.in
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_grammar.h.in      2011-08-23 13:42:42 UTC 
(rev 46328)
+++ brlcad/trunk/src/libgcv/wfobj/obj_grammar.h.in      2011-08-23 14:07:22 UTC 
(rev 46329)
@@ -1,23 +1,18 @@
 #ifndef OBJ_GRAMMAR_H
 #define OBJ_GRAMMAR_H
 
+#include "common.h"
 #include "obj_tokens.h"
+#include "obj_rules.h"
+#include "obj_token_type.h"
 
-const int TOKEN_STRING_LEN = 80;
+__BEGIN_DECLS
 
-typedef union YYSTYPE
-{
-    float real;
-    int integer;
-    int reference[3];
-    bool toggle;
-    size_t index;
-    char string[TOKEN_STRING_LEN];
-} YYSTYPE;
-
 void *ParseAlloc(void *(*mallocProc)(size_t));
-void Parse(void *parser, int tokenType, YYSTYPE tokenValue, void *scanner);
+void Parse(void *parser, int tokenType, YYSTYPE tokenValue, yyscan_t scanner);
 void ParseFree(void *p, void (*freeProc)(void*));
 void ParseTrace(FILE *stream, char *prefix);
 
+__END_DECLS
+
 #endif

Modified: brlcad/trunk/src/libgcv/wfobj/obj_grammar.yy
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_grammar.yy        2011-08-23 13:42:42 UTC 
(rev 46328)
+++ brlcad/trunk/src/libgcv/wfobj/obj_grammar.yy        2011-08-23 14:07:22 UTC 
(rev 46329)
@@ -57,16 +57,14 @@
 #include <string>
 #include <cstddef>
 #include <sys/types.h>
+#include <iostream>
 
 #define SET_SYNTAX_ERROR \
     static_cast<detail::objCombinedState*> \
-       (scanner)->parser_state.syntaxError = true;
+       (scanner->extra)->parser_state.syntaxError = true;
 
 #define YYERROR SET_SYNTAX_ERROR
 
-/* EOF symbol returned by flex */
-#define YYEOF 0
-
 #if 0
 #define DEBUG_STRINGS \
     std::cout << "\n\tworking_stringset: "; \
@@ -173,7 +171,8 @@
  * and no calls to non-reentrant functions
  *
  * returns 1 on syntax error, 0 otherwise
- * error message in ((objCombinedState*)scanner)->parser_state.err.str()
+ * returns error message in
+ *     ((objCombinedState*)scanner->extra)->parser_state.err.str()
  */
 int obj_parser_parse(yyscan_t scanner)
 {
@@ -182,14 +181,12 @@
 
     int yychar;
     YYSTYPE yyval;
-    objCombinedState *state = static_cast<objCombinedState*>(scanner);
+    objCombinedState *state = static_cast<objCombinedState*>(scanner->extra);
     parser_type parser = state->parser;
     bool &error = state->parser_state.syntaxError;
 
     error = false;
 
-    // ParseTrace(stdout, ">");
-
     while ((yychar = obj_parser_lex(&yyval, scanner)) != YYEOF) {
 #if 0
        /* print tokens received from scanner */
@@ -286,8 +283,6 @@
        Parse(parser, yychar, yyval, scanner);
     }
 
-    ParseFree(parser,free);
-
     return error;
 }
 

Modified: brlcad/trunk/src/libgcv/wfobj/obj_parser.cpp
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_parser.cpp        2011-08-23 13:42:42 UTC 
(rev 46328)
+++ brlcad/trunk/src/libgcv/wfobj/obj_parser.cpp        2011-08-23 14:07:22 UTC 
(rev 46329)
@@ -27,14 +27,17 @@
  * contents in.
  */
 
+#include "common.h"
 #include "obj_parser.h"
 #include "obj_parser_state.h"
 
 #include "obj_grammar.h"
 #include "obj_rules.h"
 
+#include <cerrno>
 #include <cstdio>
 #include <sstream>
+#include <iostream>
 
 extern int obj_parser_parse(yyscan_t);
 
@@ -110,29 +113,31 @@
 
 } /* namespace detail */
 
+__BEGIN_DECLS
 
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
 static void createParser(detail::parser_type *parser)
 {
     *parser = ParseAlloc(malloc);
 }
 
+static void destroyParser(detail::parser_type *parser)
+{
+    ParseFree(*parser, free);
+}
+
 static void createScanner(yyscan_t *scanner)
 {
-    obj_parser_lex_init(scanner);
+    BU_GETTYPE(*scanner, scanner_t);
 }
 
 static void setScannerIn(yyscan_t scanner, FILE *in)
 {
-    obj_parser_set_in(in, scanner);
+    initScanner(scanner, in);
 }
 
 static void setScannerExtra(yyscan_t scanner, detail::objCombinedState *extra)
 {
-    obj_parser_set_extra(extra, scanner);
+    obj_parser_set_extra(scanner, extra);
 }
 
 int obj_parser_create(obj_parser_t *parser)
@@ -183,8 +188,6 @@
        yyscan_t scanner;
        createScanner(&scanner);
 
-       detail::lex_sentry lsentry(scanner);
-
        setScannerIn(scanner, extra.parser_state.file_stack.back().file.get());
        setScannerExtra(scanner, &extra);
 
@@ -194,6 +197,8 @@
 
        p->last_error = extra.parser_state.err.str();
 
+       destroyParser(&((detail::objCombinedState*)scanner)->parser);
+
        if (err == 2) {
            return ENOMEM;
        }
@@ -218,15 +223,18 @@
 
 int obj_fparse(FILE *stream, obj_parser_t parser, obj_contents_t *contents)
 {
-    detail::objParser *p = static_cast<detail::objParser*>(parser.p);
+    using detail::objParser;
+    using detail::objFileContents;
+    using detail::objCombinedState;
 
+    objParser *p = static_cast<objParser*>(parser.p);
+
     int err = 0;
 
     try {
-       std::auto_ptr<detail::objFileContents>
-           sentry(new detail::objFileContents);
+       std::auto_ptr<objFileContents> sentry(new objFileContents);
 
-       detail::objCombinedState extra(p, sentry.get());
+       objCombinedState extra(p, sentry.get());
 
        if ((err = detail::set_stream(stream, extra.parser_state))) {
            return err;
@@ -235,13 +243,14 @@
        yyscan_t scanner;
        createScanner(&scanner);
 
-       detail::lex_sentry lsentry(scanner);
-
        setScannerIn(scanner, extra.parser_state.file_stack.back().file.get());
        setScannerExtra(scanner, &extra);
 
-       createParser(&((detail::objCombinedState*)scanner)->parser);
+       objCombinedState *state =
+           static_cast<objCombinedState*>(scanner->extra);
 
+       createParser(&(state->parser));
+
        err = obj_parser_parse(scanner);
 
        p->last_error = extra.parser_state.err.str();
@@ -924,9 +933,7 @@
     return 0;
 }
 
-#if defined(__cplusplus)
-} /* extern "C" */
-#endif
+__END_DECLS
 
 } /* namespace obj_parser */
 } /* namespace arl */

Modified: brlcad/trunk/src/libgcv/wfobj/obj_parser.h
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_parser.h  2011-08-23 13:42:42 UTC (rev 
46328)
+++ brlcad/trunk/src/libgcv/wfobj/obj_parser.h  2011-08-23 14:07:22 UTC (rev 
46329)
@@ -19,12 +19,11 @@
 #ifndef ARL_OBJ_PARSER_H
 #define ARL_OBJ_PARSER_H
 
+#include "common.h"
 #include <sys/types.h>
 #include <stdio.h>
 
-#if defined(__cplusplus)
-extern "C" {
-#endif
+__BEGIN_DECLS
 
 /**
  *  A structure containing a wavefront obj parser.
@@ -773,8 +772,6 @@
 size_t obj_polygonal_tnv_face_vertices(obj_contents_t contents, size_t face,
                                       const size_t (*index_arr[])[3]);
 
-#if defined(__cplusplus)
-}
-#endif
+__END_DECLS
 
 #endif

Added: brlcad/trunk/src/libgcv/wfobj/obj_rules.h
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_rules.h                           (rev 0)
+++ brlcad/trunk/src/libgcv/wfobj/obj_rules.h   2011-08-23 14:07:22 UTC (rev 
46329)
@@ -0,0 +1,28 @@
+#ifndef OBJ_RULES_H
+#define OBJ_RULES_H
+
+#include "common.h"
+#include "obj_token_type.h"
+
+enum YYCONDTYPE {
+    INITIAL,
+    id_state,
+    toggle_id_state,
+    id_list_state
+};
+#define CONDTYPE enum YYCONDTYPE
+
+#include "re2c_utils.h"
+
+__BEGIN_DECLS
+
+typedef scanner_t *yyscan_t;
+
+void obj_parser_lex_destroy(yyscan_t scanner);
+int obj_parser_lex(YYSTYPE *tokenValue, yyscan_t scanner);
+void *obj_parser_get_extra(yyscan_t scanner);
+void *obj_parser_set_extra(yyscan_t scanner, void *extra);
+
+__END_DECLS
+
+#endif /* OBJ_RULES_H */


Property changes on: brlcad/trunk/src/libgcv/wfobj/obj_rules.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Deleted: brlcad/trunk/src/libgcv/wfobj/obj_rules.ll
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_rules.ll  2011-08-23 13:42:42 UTC (rev 
46328)
+++ brlcad/trunk/src/libgcv/wfobj/obj_rules.ll  2011-08-23 14:07:22 UTC (rev 
46329)
@@ -1,402 +0,0 @@
-/*                   O B J _ R U L E S . L L
- * BRL-CAD
- *
- * Copyright (c) 2010-2011 United States Government as represented by
- * the U.S. Army Research Laboratory.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this file; see the file named COPYING for more
- * information.
- */
-%{
-/*                   O B J _ R U L E S . L L
- * Copyright (c) 2010-2011 United States Government as represented by
- * the U.S. Army Research Laboratory.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this file; see the file named COPYING for more
- * information.
- */
-
-#include "common.h"
-
-#include "bio.h"
-
-#include "obj_parser_state.h"
-
-#include <sys/types.h>
-
-#include "obj_grammar.h"
-
-#include <stdlib.h>
-#include <limits.h>
-
-/* increase token limits of at&t and mk2 lex */
-#undef YYLMAX
-#define YYLMAX 4096
-
-/* increase token limits of pclex (2x) */
-#undef F_BUFSIZE
-#define F_BUFSIZE 2048
-
-#if NO_POSIX_READ
-#define YY_INPUT(buf, result, max_size) \
-errno=0; \
-while ((result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) { \
-    if (errno != EINTR) { \
-       YY_FATAL_ERROR("input in flex scanner failed"); \
-       break; \
-    } \
-    errno=0; \
-    clearerr(yyin); \
-}
-#endif
-
-namespace arl {
-namespace obj_parser {
-namespace detail {
-
-/**
- *  convenience routines
- */
-template<typename T>
-inline static objCombinedState::parser_state_type &get_state(T scanner)
-{
-    return static_cast<objCombinedState*>(scanner)->parser_state;
-}
-
-/**
- *  Local functions
- */
-static bool split_reference(const char *s, int val[3]);
-
-} /* namespace detail */
-} /* namespace obj_parser */
-} /* namespace arl */
-
-using namespace arl::obj_parser;
-%}
-
-%option full never-interactive
-%option warn nodefault noyywrap nounput
-%option reentrant bison-bridge
-
-%x id_state
-%x toggle_id_state
-%x id_list_state
-
-vertex      v
-t_vertex    vt
-n_vertex    vn
-point       p
-line        l
-face        f
-group       g
-object      o
-smooth      s
-integer     [+-]?([[:digit:]]+)
-dseq        ([[:digit:]]+)
-dseq_opt    ([[:digit:]]*)
-frac        (({dseq_opt}"."{dseq})|{dseq}".")
-exp         ([eE][+-]?{dseq})
-exp_opt     ({exp}?)
-fsuff       [flFL]
-fsuff_opt   ({fsuff}?)
-hpref       (0[xX])
-hdseq       ([[:xdigit:]]+)
-hdseq_opt   ([[:xdigit:]]*)
-hfrac       (({hdseq_opt}"."{hdseq})|({hdseq}"."))
-bexp        ([pP][+-]?{dseq})
-dfc         (({frac}{exp_opt}{fsuff_opt})|({dseq}{exp}{fsuff_opt}))
-hfc         
(({hpref}{hfrac}{bexp}{fsuff_opt})|({hpref}{hdseq}{bexp}{fsuff_opt}))
-real        [+-]?({dfc}|{hfc})
-usemtl      usemtl
-mtllib      mtllib
-usemap      usemap
-maplib      maplib
-bevel       bevel
-c_interp    c_interp
-d_interp    d_interp
-lod         lod
-shadow_obj  shadow_obj
-trace_obj   trace_obj
-off         off
-on          on
-v_reference {integer}"/""/"?
-v_tv_reference {integer}"/"{integer}
-v_nt_reference {integer}"//"{integer}
-v_tnv_reference_list {integer}"/"{integer}"/"{integer}
-
-wspace      [ \t]
-id          [!-~]+
-newline     ["\r\n""\n"]
-comment     "#"[^"\r\n""\n"]*{newline}
-
-%%
-
-{vertex}            { return VERTEX; }
-{t_vertex}          { return T_VERTEX; }
-{n_vertex}          { return N_VERTEX; }
-{point}             { return POINT; }
-{line}              { return LINE; }
-{face}              { return FACE; }
-{group}             {
-    BEGIN(id_list_state);
-    return GROUP;
-}
-
-{object}            {
-    BEGIN(id_state);
-    return OBJECT;
-}
-
-{smooth}            {
-    return SMOOTH;
-}
-
-{integer}           {
-    yylval->integer = atoi(yytext);
-    return INTEGER;
-}
-
-{real}              {
-    yylval->real = atof(yytext);
-    return FLOAT;
-}
-
-{usemtl}            {
-    BEGIN(id_state);
-    return USEMTL;
-}
-
-{mtllib}            {
-    BEGIN(id_list_state);
-    return MTLLIB;
-}
-
-{usemap}            {
-    BEGIN(toggle_id_state);
-    return USEMAP;
-}
-
-{maplib}            {
-    BEGIN(id_list_state);
-    return MAPLIB;
-}
-
-{bevel}             {
-    return BEVEL;
-}
-
-{c_interp}          {
-    return C_INTERP;
-}
-
-{d_interp}          {
-    return D_INTERP;
-}
-
-{lod}               {
-    return LOD;
-}
-
-{shadow_obj}        {
-    BEGIN(id_state);
-    return SHADOW_OBJ;
-}
-
-{trace_obj}         {
-    BEGIN(id_state);
-    return TRACE_OBJ;
-}
-
-{on}                {
-    return ON;
-}
-
-{off}               {
-    return OFF;
-}
-
-{v_reference}       {
-    if (detail::split_reference(yytext, yylval->reference))
-       return V_REFERENCE;
-    return 0;
-}
-{v_tv_reference}    {
-    if (detail::split_reference(yytext, yylval->reference))
-       return TV_REFERENCE;
-    return 0;
-}
-{v_nt_reference}    {
-    if (detail::split_reference(yytext, yylval->reference))
-       return NV_REFERENCE;
-    return 0;
-}
-{v_tnv_reference_list} {
-    if (detail::split_reference(yytext, yylval->reference))
-       return TNV_REFERENCE;
-    return 0;
-}
-
-{wspace}            { }
-
-{comment}|{newline} {
-    ++(detail::get_state(yyextra).file_stack.back().lineno);
-    return EOL;
-}
-
-.                   { return yytext[0]; }
-
-
-<id_state>{
-
-{id}                {
-    // Keywords are valid identifiers here
-    // Goto initial state after single token
-    detail::get_state(yyextra).working_string = yytext;
-    strncpy(yylval->string, yytext, TOKEN_STRING_LEN);
-
-    BEGIN(INITIAL);
-    return ID;
-}
-
-{wspace}            { }
-
-{comment}|{newline} {
-    // Goto initial state when we hit newline
-    ++(detail::get_state(yyextra).file_stack.back().lineno);
-    BEGIN(INITIAL);
-    return EOL;
-}
-
-.                   { return yytext[0]; }
-}
-
-
-<toggle_id_state>{
-
-{off}               {
-    // off is a valid token, not an id
-    BEGIN(INITIAL);
-    return OFF;
-}
-
-{id}                {
-    // Keywords are valid identifiers here
-    // Goto initial state after single token
-    detail::get_state(yyextra).working_string = yytext;
-    strncpy(yylval->string, yytext, TOKEN_STRING_LEN);
-
-    BEGIN(INITIAL);
-    return ID;
-}
-
-{wspace}            { }
-
-{comment}|{newline} {
-    // Goto initial state when we hit newline
-    ++(detail::get_state(yyextra).file_stack.back().lineno);
-    BEGIN(INITIAL);
-    return EOL;
-}
-
-.                   { return yytext[0]; }
-}
-
-
-<id_list_state>{
-
-{id}                {
-    // Keywords are valid identifiers here
-    detail::get_state(yyextra).working_string = yytext;
-    strncpy(yylval->string, yytext, TOKEN_STRING_LEN);
-
-    return ID;
-}
-
-{wspace}            { }
-
-{comment}|{newline} {
-    // Goto initial state when we hit newline
-    ++(detail::get_state(yyextra).file_stack.back().lineno);
-    BEGIN(INITIAL);
-    return EOL;
-}
-
-.                   { return yytext[0]; }
-}
-
-
-%%
-
-namespace arl {
-namespace obj_parser {
-namespace detail {
-
-bool split_reference(const char *s, int val[3])
-{
-    memset(val, sizeof(int) * 3, 0);
-
-    char *endptr;
-    val[0] = strtol(s, &endptr, 0);
-
-    if (*endptr == 0) {
-       return true;
-    }
-
-    if (*endptr != '/') {
-       return false;
-    }
-
-    ++endptr;
-
-    val[1] = strtol(endptr, &endptr, 0);
-
-    if (*endptr == 0) {
-       return true;
-    }
-
-    if (*endptr != '/') {
-       return false;
-    }
-
-    ++endptr;
-
-    val[2] = strtol(endptr, &endptr, 0);
-
-    return (*endptr == 0);
-}
-
-
-} /* namespace detail */
-} /* namespace obj_parser */
-} /* namespace arl */
-
-/*
- * Local Variables:
- * mode: C++
- * tab-width: 8
- * indent-tabs-mode: t
- * c-file-style: "stroustrup"
- * End:
- * ex: shiftwidth=4 tabstop=8
- */

Added: brlcad/trunk/src/libgcv/wfobj/obj_rules.re
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_rules.re                          (rev 0)
+++ brlcad/trunk/src/libgcv/wfobj/obj_rules.re  2011-08-23 14:07:22 UTC (rev 
46329)
@@ -0,0 +1,392 @@
+/*                   O B J _ R U L E S . R E
+ * Copyright (c) 2010-2011 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+
+#include "common.h"
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include "bu.h"
+#include "bio.h"
+#include "obj_grammar.h"
+#include "obj_parser_state.h"
+#include "obj_rules.h"
+#include "re2c_utils.h"
+
+/* increase token limits of at&t and mk2 lex */
+#undef YYLMAX
+#define YYLMAX 4096
+
+/* increase token limits of pclex (2x) */
+#undef F_BUFSIZE
+#define F_BUFSIZE 2048
+
+#if NO_POSIX_READ
+#define YY_INPUT(buf, result, max_size) \
+errno=0; \
+while ((result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) { \
+    if (errno != EINTR) { \
+       YY_FATAL_ERROR("input in flex scanner failed"); \
+       break; \
+    } \
+    errno=0; \
+    clearerr(yyin); \
+}
+#endif
+
+namespace arl {
+namespace obj_parser {
+namespace detail {
+
+/**
+ *  convenience routines
+ */
+template<typename T>
+inline static objCombinedState::parser_state_type &get_state(T scanner)
+{
+    return static_cast<objCombinedState*>(scanner)->parser_state;
+}
+
+/**
+ *  Local functions
+ */
+static bool split_reference(const char *s, int val[3]);
+
+} /* namespace detail */
+} /* namespace obj_parser */
+} /* namespace arl */
+
+using namespace arl::obj_parser;
+
+void *obj_parser_get_extra(yyscan_t scanner)
+{
+    return scanner->extra;
+}
+
+void *obj_parser_set_extra(yyscan_t scanner, void *extra)
+{
+    scanner->extra = extra;
+}
+
+void obj_parser_lex_destroy(yyscan_t scanner)
+{
+    freeScanner(scanner);
+}
+
+int obj_parser_lex(YYSTYPE *yylval, yyscan_t scanner)
+{
+    using arl::obj_parser::detail::objCombinedState;
+
+    void *yyextra = static_cast<objCombinedState*>(scanner->extra);
+
+BEGIN_RE2C(scanner, cursor)
+
+/*!re2c
+re2c:define:YYCTYPE = char;
+re2c:define:YYCURSOR = cursor;
+re2c:yyfill:enable = 0;
+re2c:condenumprefix = "";
+re2c:define:YYSETCONDITION = BEGIN;
+
+vertex      v
+t_vertex    vt
+n_vertex    vn
+point       p
+line        l
+face        f
+group       g
+object      o
+smooth      s
+integer     [+-]?([0-9]+)
+dseq        ([0-9]+)
+dseq_opt    ([0-9]*)
+frac        (({dseq_opt}"."{dseq})|{dseq}".")
+exp         ([eE][+-]?{dseq})
+exp_opt     ({exp}?)
+fsuff       [flFL]
+fsuff_opt   ({fsuff}?)
+hpref       ('0'[xX])
+hdseq       ([0-9]+)
+hdseq_opt   ([0-9]*)
+hfrac       (({hdseq_opt}"."{hdseq})|({hdseq}"."))
+bexp        ([pP][+-]?{dseq})
+dfc         (({frac}{exp_opt}{fsuff_opt})|({dseq}{exp}{fsuff_opt}))
+hfc         
(({hpref}{hfrac}{bexp}{fsuff_opt})|({hpref}{hdseq}{bexp}{fsuff_opt}))
+real        [+-]?({dfc}|{hfc})
+usemtl      usemtl
+mtllib      mtllib
+usemap      usemap
+maplib      maplib
+bevel       bevel
+c_interp    c_interp
+d_interp    d_interp
+lod         lod
+shadow_obj  shadow_obj
+trace_obj   trace_obj
+off         off
+on          on
+v_reference {integer}"/""/"?
+v_tv_reference {integer}"/"{integer}
+v_nt_reference {integer}"//"{integer}
+v_tnv_reference_list {integer}"/"{integer}"/"{integer}
+
+wspace      [ \t]
+id          [!-~]+
+newline     ["\r\n""\n"]
+comment     "#"[^"\r\n""\n"]*{newline}
+
+<INITIAL>{vertex}            { RETURN(VERTEX); }
+<INITIAL>{t_vertex}          { RETURN(T_VERTEX); }
+<INITIAL>{n_vertex}          { RETURN(N_VERTEX); }
+<INITIAL>{point}             { RETURN(POINT); }
+<INITIAL>{line}              { RETURN(LINE); }
+<INITIAL>{face}              { RETURN(FACE); }
+<INITIAL>{group}             {
+    BEGIN(id_list_state);
+    RETURN(GROUP);
+}
+
+<INITIAL>{object}            {
+    BEGIN(id_state);
+    RETURN(OBJECT);
+}
+
+<INITIAL>{smooth}            {
+    RETURN(SMOOTH);
+}
+
+<INITIAL>{integer}           {
+    yylval->integer = atoi(yytext);
+    RETURN(INTEGER);
+}
+
+<INITIAL>{real}              {
+    yylval->real = atof(yytext);
+    RETURN(FLOAT);
+}
+
+<INITIAL>{usemtl}            {
+    BEGIN(id_state);
+    RETURN(USEMTL);
+}
+
+<INITIAL>{mtllib}            {
+    BEGIN(id_list_state);
+    RETURN(MTLLIB);
+}
+
+<INITIAL>{usemap}            {
+    BEGIN(toggle_id_state);
+    RETURN(USEMAP);
+}
+
+<INITIAL>{maplib}            {
+    BEGIN(id_list_state);
+    RETURN(MAPLIB);
+}
+
+<INITIAL>{bevel}             {
+    RETURN(BEVEL);
+}
+
+<INITIAL>{c_interp}          {
+    RETURN(C_INTERP);
+}
+
+<INITIAL>{d_interp}          {
+    RETURN(D_INTERP);
+}
+
+<INITIAL>{lod}               {
+    RETURN(LOD);
+}
+
+<INITIAL>{shadow_obj}        {
+    BEGIN(id_state);
+    RETURN(SHADOW_OBJ);
+}
+
+<INITIAL>{trace_obj}         {
+    BEGIN(id_state);
+    RETURN(TRACE_OBJ);
+}
+
+<INITIAL>{on}                {
+    RETURN(ON);
+}
+
+<INITIAL>{off}               {
+    RETURN(OFF);
+}
+
+<INITIAL>{v_reference}       {
+    if (detail::split_reference(yytext, yylval->reference))
+       RETURN(V_REFERENCE);
+    RETURN(YYEOF);
+}
+<INITIAL>{v_tv_reference}    {
+    if (detail::split_reference(yytext, yylval->reference))
+       RETURN(TV_REFERENCE);
+    RETURN(YYEOF);
+}
+<INITIAL>{v_nt_reference}    {
+    if (detail::split_reference(yytext, yylval->reference))
+       RETURN(NV_REFERENCE);
+    RETURN(YYEOF);
+}
+<INITIAL>{v_tnv_reference_list} {
+    if (detail::split_reference(yytext, yylval->reference))
+       RETURN(TNV_REFERENCE);
+    RETURN(YYEOF);
+}
+
+<INITIAL>{wspace}            { IGNORE_TOKEN; }
+
+<INITIAL>{comment}|{newline} {
+    ++(detail::get_state(yyextra).file_stack.back().lineno);
+    RETURN(EOL);
+}
+
+<INITIAL>. { IGNORE_TOKEN; }
+
+<id_state>{id}                {
+    // Keywords are valid identifiers here
+    // Goto initial state after single token
+    detail::get_state(yyextra).working_string = yytext;
+    bu_strlcpy(yylval->string, yytext, TOKEN_STRING_LEN);
+
+#if DEBUG
+    std::cout << yylval->string;
+#endif
+
+    BEGIN(INITIAL);
+    RETURN(ID);
+}
+
+<id_state>{wspace}            { IGNORE_TOKEN; }
+
+<id_state>{comment}|{newline} {
+    // Goto initial state when we hit newline
+    ++(detail::get_state(yyextra).file_stack.back().lineno);
+    BEGIN(INITIAL);
+    RETURN(EOL);
+}
+
+<id_state>. { IGNORE_TOKEN; }
+
+<toggle_id_state>{off}               {
+    // off is a valid token, not an id
+    BEGIN(INITIAL);
+    RETURN(OFF);
+}
+
+<toggle_id_state>{id}                {
+    // Keywords are valid identifiers here
+    // Goto initial state after single token
+    detail::get_state(yyextra).working_string = yytext;
+    bu_strlcpy(yylval->string, yytext, TOKEN_STRING_LEN);
+#if DEBUG
+    std::cout << yylval->string;
+#endif
+
+    BEGIN(INITIAL);
+    RETURN(ID);
+}
+
+<toggle_id_state>{wspace}            { IGNORE_TOKEN; }
+
+<toggle_id_state>{comment}|{newline} {
+    // Goto initial state when we hit newline
+    ++(detail::get_state(yyextra).file_stack.back().lineno);
+    BEGIN(INITIAL);
+    RETURN(EOL);
+}
+
+<toggle_id_state>. { IGNORE_TOKEN; }
+
+<id_list_state>{id}                {
+    // Keywords are valid identifiers here
+    detail::get_state(yyextra).working_string = yytext;
+    bu_strlcpy(yylval->string, yytext, TOKEN_STRING_LEN);
+#if DEBUG
+    std::cout << yylval->string;
+#endif
+
+    RETURN(ID);
+}
+
+<id_list_state>{wspace}            { IGNORE_TOKEN }
+
+<id_list_state>{comment}|{newline} {
+    // Goto initial state when we hit newline
+    ++(detail::get_state(yyextra).file_stack.back().lineno);
+    BEGIN(INITIAL);
+    RETURN(EOL);
+}
+
+<id_list_state>. { IGNORE_TOKEN; }
+
+*/
+END_RE2C
+
+} /* scan */
+
+namespace arl {
+namespace obj_parser {
+namespace detail {
+
+bool split_reference(const char *s, int val[3])
+{
+    memset(val, sizeof(int)*3, 0);
+
+    char *endptr;
+    val[0] = strtol(s, &endptr, 0);
+    if (*endptr == 0)
+       return true;
+
+    if (*endptr != '/')
+       return false;
+    ++endptr;
+
+    val[1] = strtol(endptr, &endptr, 0);
+    if (*endptr == 0)
+       return true;
+
+    if (*endptr != '/')
+       return false;
+    ++endptr;
+
+    val[2] = strtol(endptr, &endptr, 0);
+
+    return (*endptr == 0);
+}
+
+} /* namespace detail */
+} /* namespace obj_parser */
+} /* namespace arl */
+/*
+ * Local Variables:
+ * mode: C++
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: brlcad/trunk/src/libgcv/wfobj/obj_rules.re
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/libgcv/wfobj/obj_token_type.h
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/obj_token_type.h                              
(rev 0)
+++ brlcad/trunk/src/libgcv/wfobj/obj_token_type.h      2011-08-23 14:07:22 UTC 
(rev 46329)
@@ -0,0 +1,14 @@
+#ifndef OBJ_TOKEN_TYPE_H
+#define OBJ_TOKEN_TYPE_H
+const int TOKEN_STRING_LEN = 80;
+
+typedef union YYSTYPE
+{
+    float real;
+    int integer;
+    int reference[3];
+    bool toggle;
+    size_t index;
+    char string[TOKEN_STRING_LEN];
+} YYSTYPE;
+#endif


Property changes on: brlcad/trunk/src/libgcv/wfobj/obj_token_type.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/libgcv/wfobj/re2c_utils.c
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/re2c_utils.c                          (rev 0)
+++ brlcad/trunk/src/libgcv/wfobj/re2c_utils.c  2011-08-23 14:07:22 UTC (rev 
46329)
@@ -0,0 +1,84 @@
+#include "re2c_utils.h"
+#include "bu.h"
+
+bu_string *newString()
+{
+    return bu_vls_vlsinit();
+}
+
+int getNextLine(bu_string *buf, FILE *in)
+{
+    int len;
+
+    bu_vls_trunc(buf, 0);
+
+    len = bu_vls_gets(buf, in);
+
+    if (len < 0) {
+       return len;
+    }
+
+    bu_vls_strcat(buf, "\n");
+
+    return len + 1;
+}
+
+int loadInput(scanner_t *scanner)
+{
+    bu_string *line = scanner->currLine;
+    int len = bu_vls_strlen(line);
+    char *lineEnd = line->vls_str + len;
+
+    if (scanner->tokenStart >= lineEnd) {
+       len = getNextLine(line, scanner->in);
+       scanner->tokenStart = line->vls_str;
+    }
+
+    return len;
+}
+
+char *copyCurrTokenText(bu_string *tokenString, scanner_t *scanner)
+{
+    int tokenLen = *scanner->cursor_p - scanner->tokenStart;
+
+    bu_vls_strncpy(tokenString, scanner->tokenStart, tokenLen);
+
+    return tokenString->vls_str;
+}
+
+void setStartCondition(scanner_t *scanner, const CONDTYPE sc)
+{
+    scanner->startCondition = sc;
+}
+
+CONDTYPE getStartCondition(scanner_t *scanner)
+{
+    return scanner->startCondition;
+}
+
+void initScanner(scanner_t *scanner, FILE *in)
+{
+    setStartCondition(scanner, INITIAL);
+
+    scanner->tokenText = newString();
+
+    scanner->in = in;
+
+    scanner->currLine = newString();
+    getNextLine(scanner->currLine, scanner->in);
+
+    scanner->tokenStart = scanner->currLine->vls_str;
+}
+
+void freeScanner(scanner_t *scanner)
+{
+    fclose(scanner->in);
+    bu_vls_vlsfree(scanner->currLine);
+    bu_vls_vlsfree(scanner->tokenText);
+
+    scanner->extra = NULL;
+    scanner->tokenStart = NULL;
+    scanner->cursor_p = NULL;
+
+    bu_free(scanner, "freeScanner");
+}


Property changes on: brlcad/trunk/src/libgcv/wfobj/re2c_utils.c
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/libgcv/wfobj/re2c_utils.h
===================================================================
--- brlcad/trunk/src/libgcv/wfobj/re2c_utils.h                          (rev 0)
+++ brlcad/trunk/src/libgcv/wfobj/re2c_utils.h  2011-08-23 14:07:22 UTC (rev 
46329)
@@ -0,0 +1,143 @@
+/* Interface to simplify usage of re2c by providing support for:
+ *  - automatic input buffer management
+ *        - Just specify a FILE* and the scanner will read from the input file
+ *          as needed and save its place in the input between calls to the
+ *          scan function.
+ *
+ *  - end-of-input token
+ *        - The YYEOF symbol is defined, and is returned by the scan function 
+ *          when the end of the input file is reached.
+ *
+ *  - ignoring tokens
+ *        - Use IGNORE_TOKEN in an action body to ignore the recognized token
+ *          and continue scanning.
+ *
+ *  - (f)lex-like yytext string
+ *        - char *yytext will exist as a local buffer that holds the text of 
the
+ *          current token.
+ *
+ *  - (f)lex-like BEGIN statement
+ *        - You can use BEGIN(condition) to set the current start condition.
+ *
+ * There are a few requirements to get this support:
+ *  - You need to include this header in the re2c input file and link to the
+ *    corresponding object file.
+ *
+ *  - Your scanning function needs to return int (the token id). To return the
+ *    token id, you must use RETURN(id) rather than simply return id.
+ *
+ *  - A scanner_t* that you have initialized with initScanner(scanner_t*, 
FILE*)
+ *    (and that you can deconstruct with freeScanner(scanner_t*)) must be
+ *    available inside your scan function.
+ *
+ *  - You need to wrap the re2c comment block between the macros
+ *    BEGIN_RE2C(scanner, cursor) and END_RE2C, where scanner is the name of
+ *    a scanner_t*, and cursor is the name of the char* given to re2c via:
+ *        re2c:define:YYCURSOR = cursor;
+ *
+ *  - To use the (f)lex-like BEGIN(condition) statement you need to define the
+ *    CONDTYPE symbol before including this header:
+ *        enum YYCONDTYPE {
+ *            INITIAL,
+ *            ...other states...
+ *        };
+ *        #define CONDTYPE enum YYCONDTYPE
+ */
+
+#ifndef RE2C_UTILS_H
+#define RE2C_UTILS_H
+
+#include "common.h"
+#include "bu.h"
+
+__BEGIN_DECLS
+
+/* end of file token - since parser generators start defining token ids at 0,
+ * -1 is the least likely value to be used by an existing token
+ */
+#define YYEOF -1
+
+#ifndef CONDTYPE
+enum YYCONDTYPE { INITIAL };
+#define CONDTYPE enum YYCONDTYPE
+#endif
+
+typedef struct bu_vls bu_string;
+
+typedef struct {
+    void *extra; /* user data */
+    FILE *in;
+    bu_string *currLine;
+    bu_string *tokenText;
+    char *tokenStart;
+    char *marker;
+    char **cursor_p;
+    CONDTYPE startCondition;
+} scanner_t;
+
+void initScanner(scanner_t *scanner, FILE *in);
+void freeScanner(scanner_t *scanner);
+
+#define YYMARKER re2c_scanner->marker
+
+/* support for (f)lex "start conditions" */
+#define BEGIN(sc) setStartCondition(re2c_scanner, sc)
+#define YYGETCONDITION() getStartCondition(re2c_scanner)
+
+void setStartCondition(scanner_t *scanner, const CONDTYPE sc);
+CONDTYPE getStartCondition(scanner_t *scanner);
+
+
+/* text processing support */
+bu_string *newString();
+int getNextLine(bu_string *buf, FILE *in);
+int loadInput(scanner_t *scanner);
+
+
+/* support for (f)lex token string */
+#define FREE_TOKEN_TEXT bu_vls_vlsfree(tokenString);
+#define yytext (copyCurrTokenText(tokenString, re2c_scanner))
+
+char *copyCurrTokenText(bu_string *tokenString, scanner_t *scanner);
+
+
+/* update/save stream position */
+#define UPDATE_START re2c_scanner->tokenStart = *re2c_scanner->cursor_p;
+
+
+/* use inside action to skip curr token and continue scanning */
+#define IGNORE_TOKEN \
+    UPDATE_START; \
+    continue;
+
+
+/* use inside action to return tokenID to caller */
+#define RETURN(tokenID) \
+    FREE_TOKEN_TEXT \
+    UPDATE_START; \
+    return tokenID;
+
+
+#define BEGIN_RE2C(scanner, yycursor) \
+    /* define the stuff used by helper macros */ \
+    scanner_t *re2c_scanner = scanner; \
+    bu_string *tokenString; \
+    char *yycursor; \
+\
+    if (loadInput(re2c_scanner) < 0) { \
+       return YYEOF; \
+    } \
+\
+    yycursor = re2c_scanner->tokenStart; \
+    re2c_scanner->cursor_p = &yycursor; \
+\
+    tokenString = newString(); \
+\
+    /* create implicit label */ \
+    while (1) {
+
+#define END_RE2C } /* while (1) */
+
+__END_DECLS
+
+#endif /* RE2C_UTILS_H */


Property changes on: brlcad/trunk/src/libgcv/wfobj/re2c_utils.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, 
user administration capabilities and model configuration. Take 
the hassle out of deploying and managing Subversion and the 
tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
_______________________________________________
BRL-CAD Source Commits mailing list
brlcad-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to