Parse two-chars tokens (like <= or &&) as well

master
Yevgen Muntyan 2006-02-26 18:56:19 -06:00
parent f733f66f54
commit 6be94c2218
4 changed files with 115 additions and 20 deletions

View File

@ -268,16 +268,16 @@
</kdevdoctreeview> </kdevdoctreeview>
<kdevfilecreate> <kdevfilecreate>
<filetypes> <filetypes>
<type icon="source" ext="g" name="GAP source" create="template" > <type icon="source" ext="g" create="template" name="GAP source" >
<descr>A new empty GAP source file</descr> <descr>A new empty GAP source file</descr>
</type> </type>
<type icon="source_cpp" ext="cpp" name="C++ Source" create="template" > <type icon="source_cpp" ext="cpp" create="template" name="C++ Source" >
<descr>A new empty C++ file.</descr> <descr>A new empty C++ file.</descr>
</type> </type>
<type icon="source_h" ext="h" name="C/C++ Header" create="template" > <type icon="source_h" ext="h" create="template" name="C/C++ Header" >
<descr>A new empty header file for C/C++.</descr> <descr>A new empty header file for C/C++.</descr>
</type> </type>
<type icon="source_c" ext="c" name="C Source" create="template" > <type icon="source_c" ext="c" create="template" name="C Source" >
<descr>A new empty C file.</descr> <descr>A new empty C file.</descr>
</type> </type>
</filetypes> </filetypes>

View File

@ -241,21 +241,32 @@ ms_lex_parse_word (MSLex *lex,
} }
static int #define THIS (lex->input[lex->ptr])
ms_lex_parse_dot (MSLex *lex, #define NEXT (lex->input[lex->ptr+1])
G_GNUC_UNUSED MSParser *parser)
{
g_assert (lex->input[lex->ptr] == '.');
if (lex->input[lex->ptr+1] == '.') #define RETURN1(what) \
{ G_STMT_START { \
lex->ptr += 2; lex->ptr += 1; \
return TWODOTS; return what; \
} } G_STMT_END
lex->ptr++; #define CHECK1(c_, what_) \
return '.'; G_STMT_START { \
} if (THIS == c_) \
RETURN2 (what_); \
} G_STMT_END
#define RETURN2(what) \
G_STMT_START { \
lex->ptr += 2; \
return what; \
} G_STMT_END
#define CHECK2(c1_, c2_, what_) \
G_STMT_START { \
if (THIS == c1_ && NEXT == c2_) \
RETURN2 (what_); \
} G_STMT_END
int int
@ -287,8 +298,15 @@ _ms_script_yylex (MSParser *parser)
if (IS_LETTER (c) || c == '_') if (IS_LETTER (c) || c == '_')
return ms_lex_parse_word (lex, parser); return ms_lex_parse_word (lex, parser);
if (c == '.') CHECK2 ('.', '.', TWODOTS);
return ms_lex_parse_dot (lex, parser); CHECK2 ('=', '=', EQ);
CHECK2 ('!', '=', NEQ);
CHECK2 ('&', '&', AND);
CHECK2 ('|', '|', OR);
CHECK2 ('<', '=', LE);
CHECK2 ('>', '=', GE);
CHECK1 ('!', NOT);
lex->ptr++; lex->ptr++;
return c; return c;

View File

@ -1,4 +1,4 @@
EXTRA_PROGRAMS += medit mterm markup termbuffer testfileview testpaned testpanedfileview miniglade langparser testobject testfold EXTRA_PROGRAMS += medit mterm markup termbuffer testfileview testpaned testpanedfileview miniglade langparser testobject testfold script
bin_PROGRAMS = bin_PROGRAMS =
@ -98,6 +98,18 @@ nodist_langparser_SOURCES = $(nodist_libmoo_sources)
endif endif
##############################################################################
## script
##
script_LDFLAGS = $(ldflags)
script_LDADD = $(ldadd)
script_SOURCES = tests/script.c
if !MOO_BUILD_LIB
script_SOURCES += $(libmoo_sources)
nodist_script_SOURCES = $(nodist_libmoo_sources)
endif
############################################################################## ##############################################################################
## mterm ## mterm
## ##

65
tests/script.c Normal file
View File

@ -0,0 +1,65 @@
/*
* script.c
*
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#define MOOEDIT_COMPILATION
#include "mooutils/mooscript/mooscript-parser.h"
#include <gtk/gtk.h>
#include <string.h>
static void usage (const char *prg)
{
g_error ("usage:\t%s -f <file>\n\t%s <script>", prg, prg);
}
int main (int argc, char *argv[])
{
char *script;
MSNode *node;
MSValue *val;
MSContext *ctx;
gtk_init (&argc, &argv);
if (argc < 2)
usage (argv[0]);
if (argc == 2)
{
script = argv[1];
}
else if (strcmp (argv[1], "-f"))
{
usage (argv[0]);
}
else
{
GError *error = NULL;
if (!g_file_get_contents (argv[2], &script, NULL, &error))
g_error ("%s", error->message);
}
node = ms_script_parse (script);
if (!node)
g_error ("could not parse script");
ctx = ms_context_new ();
val = ms_node_eval (node, ctx);
if (!val)
g_print ("error: %s\n", ms_context_get_error_msg (ctx));
else
g_print ("success: %s\n", ms_value_print (val));
}