rlparse.kh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
  3. */
  4. /* This file is part of Ragel.
  5. *
  6. * Ragel is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Ragel is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ragel; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _RLPARSE_H
  21. #define _RLPARSE_H
  22. #include <iostream>
  23. #include "avltree.h"
  24. #include "parsedata.h"
  25. /* Import scanner tokens. */
  26. #define IMP_Word 128
  27. #define IMP_Literal 129
  28. #define IMP_UInt 130
  29. #define IMP_Define 131
  30. /* This is used for tracking the include files/machine pairs. */
  31. struct IncludeHistoryItem
  32. {
  33. IncludeHistoryItem( const char *fileName, const char *sectionName )
  34. : fileName(fileName), sectionName(sectionName) {}
  35. const char *fileName;
  36. const char *sectionName;
  37. };
  38. typedef Vector<IncludeHistoryItem> IncludeHistory;
  39. struct Parser
  40. {
  41. %%{
  42. parser Parser;
  43. # General tokens.
  44. token TK_Word, TK_Literal, TK_Number, TK_EndSection, TK_UInt, TK_Hex,
  45. TK_Word, TK_Literal, TK_DotDot, TK_ColonGt, TK_ColonGtGt, TK_LtColon,
  46. TK_Arrow, TK_DoubleArrow, TK_StarStar, TK_ColonEquals, TK_NameSep,
  47. TK_BarStar, TK_DashDash;
  48. # Conditions.
  49. token TK_StartCond, TK_AllCond, TK_LeavingCond;
  50. # State embedding actions.
  51. token TK_Middle;
  52. # Global error actions.
  53. token TK_StartGblError, TK_AllGblError, TK_FinalGblError,
  54. TK_NotFinalGblError, TK_NotStartGblError, TK_MiddleGblError;
  55. # Local error actions.
  56. token TK_StartLocalError, TK_AllLocalError, TK_FinalLocalError,
  57. TK_NotFinalLocalError, TK_NotStartLocalError, TK_MiddleLocalError;
  58. # EOF Action embedding.
  59. token TK_StartEOF, TK_AllEOF, TK_FinalEOF, TK_NotFinalEOF, TK_NotStartEOF,
  60. TK_MiddleEOF;
  61. # To State Actions.
  62. token TK_StartToState, TK_AllToState, TK_FinalToState, TK_NotFinalToState,
  63. TK_NotStartToState, TK_MiddleToState;
  64. # In State Actions.
  65. token TK_StartFromState, TK_AllFromState, TK_FinalFromState,
  66. TK_NotFinalFromState, TK_NotStartFromState, TK_MiddleFromState;
  67. # Regular expression tokens. */
  68. token RE_Slash, RE_SqOpen, RE_SqOpenNeg, RE_SqClose, RE_Dot, RE_Star,
  69. RE_Dash, RE_Char;
  70. # Tokens specific to inline code.
  71. token IL_WhiteSpace, IL_Comment, IL_Literal, IL_Symbol;
  72. # Keywords.
  73. token KW_Machine, KW_Include, KW_Import, KW_Write, KW_Action, KW_AlphType,
  74. KW_Range, KW_GetKey, KW_Include, KW_Write, KW_Machine, KW_InWhen,
  75. KW_When, KW_OutWhen, KW_Eof, KW_Err, KW_Lerr, KW_To, KW_From,
  76. KW_Export, KW_PrePush, KW_PostPop, KW_Length;
  77. # Specials in code blocks.
  78. token KW_Break, KW_Exec, KW_Hold, KW_PChar, KW_Char, KW_Goto, KW_Call,
  79. KW_Ret, KW_CurState, KW_TargState, KW_Entry, KW_Next, KW_Exec,
  80. KW_Variable, KW_Access;
  81. }%%
  82. %% write instance_data;
  83. void init();
  84. int parseLangEl( int type, const Token *token );
  85. Parser( const char *fileName, char *sectionName, InputLoc &sectionLoc )
  86. : sectionName(sectionName)
  87. {
  88. pd = new ParseData( fileName, sectionName, sectionLoc );
  89. exportContext.append( false );
  90. includeHistory.append( IncludeHistoryItem(
  91. fileName, sectionName ) );
  92. }
  93. int token( InputLoc &loc, int tokId, char *tokstart, int toklen );
  94. void tryMachineDef( InputLoc &loc, char *name,
  95. MachineDef *machineDef, bool isInstance );
  96. /* Report an error encountered by the parser. */
  97. ostream &parse_error( int tokId, Token &token );
  98. ParseData *pd;
  99. /* The name of the root section, this does not change during an include. */
  100. char *sectionName;
  101. NameRef nameRef;
  102. NameRefList nameRefList;
  103. Vector<bool> exportContext;
  104. IncludeHistory includeHistory;
  105. Parser *prev, *next;
  106. };
  107. %% write token_defs;
  108. #endif