pr_lex.l
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:14k
- %{
- /*
- * pr_lex.l
- *
- * Resource compiler lexical analyser
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: pr_lex.l,v $
- * Revision 1.24 1999/10/29 11:44:25 robertj
- * Fixed #ifdef conditional around the wrong way.
- *
- * Revision 1.24 1999/10/29 11:44:25 robertj
- * Fixed #ifdef conditional around the wrong way.
- *
- * Revision 1.23 1999/10/19 07:08:23 robertj
- * Added #ifdef/#ifndef condition compile functionality
- *
- * Revision 1.22 1999/02/06 08:45:33 robertj
- * egcs compatibility
- *
- * Revision 1.21 1998/09/26 01:24:15 robertj
- * Added open source license
- *
- * Revision 1.20 1998/09/26 00:40:27 robertj
- * Fixed linkage mismatch of lex function.
- *
- * Revision 1.19 1998/09/14 13:48:05 robertj
- * Allowed suppression of some warnings.
- *
- * Revision 1.18 1997/12/12 08:44:25 robertj
- * Update to currect version of PWLIB
- *
- * Revision 1.17 1996/01/23 13:32:45 robertj
- * More changes for editor
- *
- * Revision 1.16 1995/12/23 03:53:39 robertj
- * Added extra collections for editor use.
- *
- * Revision 1.15 1995/12/10 12:12:05 robertj
- * Changes to support graphical resource file editor.
- *
- * Revision 1.14 1995/08/24 12:48:13 robertj
- * Added general control type for arbitrary dialog controls.
- *
- * Revision 1.13 1995/07/31 12:30:13 robertj
- * Changed all generated resource IDs to be in the same "number space". This
- * is to avoid problems with IDs being used for more than one type of resource
- * eg a menu item and an image for a toolbar.
- * Fixed the generated #line commands to include a relative path instead of an
- * absolute one.
- *
- * Revision 1.12 1995/01/27 11:25:43 robertj
- * Added pattern resource.
- *
- * Revision 1.11 1994/12/12 13:14:04 robertj
- * Changed RealEdit to FloatEdit.
- *
- * Revision 1.10 1994/12/05 11:38:01 robertj
- * Added #line to generated code.
- *
- * Revision 1.9 1994/10/23 06:07:12 robertj
- * Shortened OS error assert.
- *
- * Revision 1.8 1994/07/02 03:28:52 robertj
- * Fixed accidentaly removed header keyword.
- * Fixed comment parsing problem.
- *
- * Revision 1.6 1994/04/20 12:47:01 robertj
- * *** empty log message ***
- *
- * Revision 1.5 1994/04/01 14:29:16 robertj
- * New format PRC file
- *
- * Revision 1.4 1993/08/20 21:31:56 robertj
- * Included ctype for unix compatibility.
- *
- * Revision 1.3 1993/07/16 12:48:13 robertj
- * Added slash star style comments.
- * General formatting.
- *
- * Revision 1.2 1993/07/15 04:38:31 robertj
- * Rationalised lex.h, parse.h and main.h out of existance.
- * Fixed literal string so can have embedded " characters.
- *
- * Revision 1.1 1993/07/03 06:01:44 robertj
- * Initial revision
- *
- * Revision 1.2 1993/06/24 19:48:57 craigs
- * Added support for hash comments
- *
- * Revision 1.1 1993/06/24 19:23:17 craigs
- * Initial revision
- *
- */
- #include <ptlib.h>
- #include "pwrc.h"
- #include "pr_gramm.h"
- #include <ctype.h>
- extern FILE * yyin;
- extern int yyparse();
- extern ResourceFile * current_file;
- extern ostream * inline_src;
- extern ostream * inline_hdr;
- ///////////////////////////////////////////
- //
- // handling for "include" files
- //
- PDECLARE_CLASS(LexState, PObject)
- public:
- LexState(const PFilePath & fn)
- : filename(fn) { line_number = 1; }
- Comparison Compare(const PObject & obj) const
- { return filename.Compare(((const LexState &)obj).filename); }
- YY_BUFFER_STATE yy_state;
- unsigned line_number;
- PFilePath filename;
- PDirectory dir;
- };
- PSTACK(LexStack, LexState);
- static LexStack LexFile;
- static int in_define;
- static int in_multiline_comment;
- PStringArray IncludePath;
- ResourceFile::ResourceFile(PFile & prcfile, BOOL suppress,
- const PString & predefinedSymbols)
- {
- nextResourceId = 10000;
- fatals = warnings = 0;
- suppressInfoWarnings = suppress;
- PStringArray symbols = predefinedSymbols.Lines();
- for (PINDEX i = 0; i < symbols.GetSize(); i++)
- defineDict.SetAt(symbols[i], new DefineValue(1, FALSE));
- inline_src = &src_inline;
- inline_hdr = &hdr_inline;
- for (PINDEX type = 0; type < ResourceList::NumResTypes; type++) {
- resources[type].type = (ResourceList::ResTypes)type;
- resources[type].DisallowDeleteObjects();
- }
- resourceDict[ResourceList::StringRes] = &stringDict;
- resourceDict[ResourceList::MenubarRes] = &menubarDict;
- resourceDict[ResourceList::LayoutRes] = &dialogDict;
- resourceDict[ResourceList::IconRes] = &iconDict;
- resourceDict[ResourceList::CursorRes] = &cursorDict;
- resourceDict[ResourceList::PatternRes] = &patternDict;
- resourceDict[ResourceList::ImageRes] = &imageDict;
- resourceDict[ResourceList::DataRes] = &dataDict;
- current_file = this;
- in_define = 0;
- LexFile.Push(new LexState(prcfile.GetFilePath()));
- yyin = _fdopen(prcfile.GetHandle(), "r");
- PAssertNULL(yyin);
- yyparse();
- }
- ostream & operator<<(ostream & out, const StdError & e)
- {
- if (e.e == Info && current_file->suppressInfoWarnings) {
- static PStringStream dummy;
- dummy = "";
- return dummy;
- }
- out << e.l.filename << '(' << e.l.number << ") : ";
- if (e.e == Fatal) {
- current_file->fatals++;
- out << "error";
- }
- else {
- current_file->warnings++;
- out << "warning";
- }
- return out << ": ";
- }
- void LineNumber::Set()
- {
- if (LexFile.IsEmpty()) {
- filename = "";
- number = 0;
- }
- else {
- filename = LexFile.Top().filename;
- number = LexFile.Top().line_number;
- }
- }
- extern "C" int yywrap()
- {
- return 1;
- }
- %}
- %x hash_include hash_inline_src hash_inline_hdr comment_lines conditional_compilation
- %%
- [Dd][Ee][Ff][Pp][Uu][Ss][Hh][Bb][Uu][Tt][Tt][Oo][Nn] { return DEFPUSHBUTTON; }
- [Aa][Cc][Cc][Ee][Ll][Ee][Rr][Aa][Tt][Oo][Rr][Ss] { return ACCELERATORS; }
- [Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd][Bb][Oo][Xx] { return PASSWORDBOX; }
- [Mm][Oo][Dd][Aa][Ll][Dd][Ii][Aa][Ll][Oo][Gg] { return MODALDIALOG; }
- [Rr][Aa][Dd][Ii][Oo][Bb][Uu][Tt][Tt][Oo][Nn] { return RADIOBUTTON; }
- [Mm][Uu][Ll][Tt][Ii][Ss][Ee][Ll][Ee][Cc][Tt] { return MULTISELECT; }
- [Mm][Uu][Ll][Tt][Ii][Cc][Oo][Ll][Uu][Mm][Nn] { return MULTICOLUMN; }
- [Ss][Tt][Rr][Ii][Nn][Gg][Ll][Ii][Ss][Tt] { return STRINGLIST; }
- [Pp][Uu][Ss][Hh][Bb][Uu][Tt][Tt][Oo][Nn] { return PUSHBUTTON; }
- [Ss][Tt][Aa][Tt][Ii][Cc][Ii][Cc][Oo][Nn] { return STATICICON; }
- [Cc][Ee][Nn][Tt][Rr][Ee][Tt][Ee][Xx][Tt] { return CENTRETEXT; }
- [Cc][Ee][Nn][Tt][Ee][Rr][Tt][Ee][Xx][Tt] { return CENTRETEXT; }
- [Hh][Ss][Cc][Rr][Oo][Ll][Ll][Bb][Aa][Rr] { return HSCROLLBAR; }
- [Vv][Ss][Cc][Rr][Oo][Ll][Ll][Bb][Aa][Rr] { return VSCROLLBAR; }
- [Tt][Ee][Xx][Tt][Ee][Dd][Ii][Tt][Oo][Rr] { return TEXTEDITOR; }
- [Dd][Ii][Mm][Ee][Nn][Ss][Ii][Oo][Nn][Ss] { return DIMENSIONS; }
- [Dd][Ii][Mm] { return DIMENSIONS; }
- [Rr][Ii][Gg][Hh][Tt][Tt][Ee][Xx][Tt] { return RIGHTTEXT; }
- [Ss][Tt][Aa][Tt][Ii][Cc][Bb][Oo][Xx] { return STATICBOX; }
- [Ss][Ee][Pp][Aa][Rr][Aa][Tt][Oo][Rr] { return SEPARATOR; }
- [Cc][Hh][Oo][Ii][Cc][Ee][Bb][Oo][Xx] { return CHOICEBOX; }
- [Ff][Ll][Oo][Aa][Tt][Ee][Dd][Ii][Tt] { return FLOATEDIT; }
- [Cc][Hh][Ee][Cc][Kk]3[Ww][Aa][Yy] { return CHECK3WAY; }
- [Cc][Hh][Ee][Cc][Kk][Bb][Oo][Xx] { return CHECKBOX; }
- [Ll][Ee][Ff][Tt][Tt][Ee][Xx][Tt] { return LEFTTEXT; }
- [Pp][Oo][Ss][Ii][Tt][Ii][Oo][Nn] { return POSITION; }
- [Pp][Oo][Ss] { return POSITION; }
- [Cc][Oo][Mm][Bb][Oo][Bb][Oo][Xx] { return COMBOBOX; }
- [Rr][Ee][Ss][Oo][Uu][Rr][Cc][Ee] { return RESOURCE; }
- [Oo][Pp][Tt][Ii][Oo][Nn][Ss] { return OPTIONS; }
- [Ee][Dd][Ii][Tt][Bb][Oo][Xx] { return EDITBOX; }
- [Ii][Nn][Tt][Ee][Dd][Ii][Tt] { return INTEDIT; }
- [Ll][Ii][Ss][Tt][Bb][Oo][Xx] { return LISTBOX; }
- [Mm][Ee][Nn][Uu][Bb][Aa][Rr] { return MENUBAR; }
- [Pp][Aa][Tt][Tt][Ee][Rr][Nn] { return PATTERN_token; }
- [Hh][Oo][Tt][Ss][Pp][Oo][Tt] { return HOTSPOT; }
- [Aa][Nn][Dd][Mm][Aa][Ss][Kk] { return ANDMASK; }
- [Xx][Oo][Rr][Mm][Aa][Ss][Kk] { return XORMASK; }
- [Cc][Oo][Ll][Oo][Uu][Rr][Ss] { return COLOURS; }
- [Cc][Oo][Ll][Oo][Rr][Ss] { return COLOURS; }
- [Dd][Ee][Ff][Aa][Uu][Ll][Tt] { return DEFAULT; }
- [Cc][Oo][Nn][Tt][Rr][Oo][Ll] { return CONTROL; }
- [Hh][Ee][Aa][Dd][Ee][Rr] { return HEADER; }
- [Ww][Ii][Nn][Dd][Oo][Ww] { return WINDOW; }
- [Nn][Oo][Tt][Ii][Ff][Yy] { return NOTIFY; }
- [Ll][Aa][Yy][Oo][Uu][Tt] { return LAYOUT; }
- [Ss][Tt][Rr][Ii][Nn][Gg] { return STRING; }
- [Dd][Ii][Aa][Ll][Oo][Gg] { return DIALOG; }
- [Ss][Oo][Rr][Tt][Ee][Dd] { return SORTED; }
- [Cc][Uu][Rr][Ss][Oo][Rr] { return CURSOR; }
- [Pp][Ii][Xx][Ee][Ll][Ss] { return PIXELS; }
- [Cc][Aa][Nn][Cc][Ee][Ll] { return CANCEL; }
- [Ll][Ii][Mm][Ii][Tt][Ss] { return LIMITS; }
- [Tt][Ii][Tt][Ll][Ee] { return TITLE; }
- [Ii][Mm][Aa][Gg][Ee] { return IMAGE; }
- [Bb][Ee][Gg][Ii][Nn] { return BEGIN_token; }
- [Vv][Aa][Ll][Uu][Ee] { return VALUE; }
- [Cc][Ll][Aa][Ss][Ss] { return CLASS; }
- [Ff][Ii][Ee][Ll][Dd] { return FIELD; }
- [Ff][Oo][Nn][Tt] { return FONT; }
- [Mm][Ee][Nn][Uu] { return MENU; }
- [Ii][Cc][Oo][Nn] { return ICON; }
- [Ii][Tt][Ee][Mm] { return ITEM; }
- [Hh][Ee][Ll][Pp] { return HELP; }
- [Ee][Nn][Dd] { return END; }
- [Ii][Dd] { return ID; }
- [Oo][Kk] { return OK; }
- ^"#inline"[ t]+"src".* {
- BEGIN(hash_inline_src);
- *inline_src << LineNumber();
- }
- <hash_inline_src>^"#inline"[ t]+"end".* { BEGIN(0); }
- <hash_inline_src>^[^n]* { *inline_src << yytext; }
- <hash_inline_src>n {
- LexFile.Top().line_number++;
- *inline_src << 'n';
- }
- ^"#inline"[ t]+"hdr".* {
- BEGIN(hash_inline_hdr);
- *inline_hdr << LineNumber();
- }
- <hash_inline_hdr>^"#inline"[ t]+"end".* { BEGIN(0); }
- <hash_inline_hdr>^[^n]* { *inline_hdr << yytext; }
- <hash_inline_hdr>n {
- LexFile.Top().line_number++;
- *inline_hdr << 'n';
- }
- ^#include { BEGIN(hash_include); }
- <hash_include>[ t] { }
- <hash_include>[^ tn]* {
- PString text(yytext);
- PINDEX end = P_MAX_INDEX;
- PINDEX start = text.FindOneOf(""<");
- if (start != P_MAX_INDEX)
- end = text.Find(text[start] == '"' ? '"' : '>', start+1);
- if (start == P_MAX_INDEX || end == P_MAX_INDEX)
- PError << StdError(Warning) << "badly formed include.n";
- else {
- PString fname = text(start+1, end-1);
- FILE * file = NULL;
- if (text[start] == '"')
- file = fopen(fname, "r");
- for (PINDEX i = 0; file == NULL && i < IncludePath.GetSize(); i++) {
- if (PDirectory::Change(IncludePath[i]))
- file = fopen(fname, "r");
- else
- PError << StdError(Warning) << "invalid path ""
- << IncludePath[i] << "".n";
- if (file == NULL)
- PAssertOS(LexFile.Top().dir.Change());
- }
- if (file == NULL)
- PError << StdError(Warning)
- << "include file " << fname << " not found.n";
- else {
- LexFile.Top().yy_state = YY_CURRENT_BUFFER;
- LexFile.Push(new LexState(fname));
- yyin = file;
- yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
- }
- }
- BEGIN(INITIAL);
- }
- <<EOF>> {
- delete LexFile.Pop();
- if (LexFile.IsEmpty())
- yyterminate();
- else {
- yy_switch_to_buffer(LexFile.Top().yy_state);
- PAssertOS(LexFile.Top().dir.Change());
- }
- }
- ^#define {
- in_define = 1;
- return HASH_DEFINE;
- }
- ^#ifdef[ t]+[^ tn]* {
- PString text(yytext);
- if (!current_file->IsNameDefined(text.Mid(text.Find(' ')).Trim()))
- BEGIN(conditional_compilation);
- }
- ^#ifndef[ t]+[^ tn]* {
- PString text(yytext);
- if (current_file->IsNameDefined(text.Mid(text.Find(' ')).Trim()))
- BEGIN(conditional_compilation);
- }
- <conditional_compilation>^#endif.*n {
- BEGIN(INITIAL);
- }
- <conditional_compilation>n {
- LexFile.Top().line_number++;
- }
- <conditional_compilation>. { }
- ^#endif.*n { }
- "//" {
- in_multiline_comment = FALSE;
- BEGIN(comment_lines);
- }
- "/*" {
- in_multiline_comment = TRUE;
- BEGIN(comment_lines);
- }
- <comment_lines>"*/" {
- if (in_multiline_comment)
- BEGIN(INITIAL);
- }
- <comment_lines>n {
- LexFile.Top().line_number++;
- if (!in_multiline_comment)
- BEGIN(INITIAL);
- }
- <comment_lines>. { }
- "@" { return '@'; }
- "{" { return '{'; }
- "}" { return '}'; }
- ";" { return ';'; }
- "(" { return '('; }
- ")" { return ')'; }
- "+" { return '+'; }
- "-" { return '-'; }
- "*" { return '*'; }
- "/" { return '/'; }
- "," { return ','; }
- ":" { return ':'; }
- "=" { return '='; }
- "([^"n]|(\"))*" {
- yylval.sval = new PString(yytext);
- return LITERAL;
- }
- "0x"[0-9a-fA-F][0-9a-fA-F]* {
- yylval.ival = strtol(yytext+2, NULL, 16);
- return INTEGER;
- }
- "0"[0-7]* {
- yylval.ival = strtol(yytext, NULL, 8);
- return INTEGER;
- }
- [1-9][0-9]* {
- yylval.ival = strtol(yytext, NULL, 10);
- return INTEGER;
- }
- [a-zA-Z_][a-zA-Z0-9_]* {
- PString name = yytext;
- if (in_define ||
- current_file->defineDict.GetAt(name) == NULL ||
- !current_file->defineDict[name].IsString()) {
- yylval.sval = new PString(name);
- return IDENTIFIER;
- }
- else {
- PString tmp_str = current_file->defineDict[name];
- yylval.sval = new PString(tmp_str);
- return LITERAL;
- }
- }
- [ tr] { }
- n {
- LexFile.Top().line_number++;
- if (in_define) {
- in_define = 0;
- return END_DEFINE;
- }
- }
- . { PError << StdError(Warning) << "unknown token " << yytext << endl; }
- %%
- /* End PR_LEX.L */