CE Token.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /******************************************************************************/
  2. #if EE_PRIVATE
  3. namespace Edit{
  4. /******************************************************************************/
  5. enum TOKEN_TYPE : Byte
  6. {
  7. TOKEN_NONE , // space or empty (end of line / out of range)
  8. TOKEN_REMOVE , // removed and to be cleaned
  9. TOKEN_OPERATOR,
  10. TOKEN_KEYWORD ,
  11. TOKEN_CODE ,
  12. TOKEN_COMMENT ,
  13. TOKEN_TEXT8 ,
  14. TOKEN_TEXT16 ,
  15. TOKEN_CHAR8 ,
  16. TOKEN_CHAR16 ,
  17. TOKEN_NUMBER ,
  18. TOKEN_PREPROC , // preprocessor
  19. TOKEN_MACRO , // used only in ColorTheme (and not in source)
  20. TOKEN_ENUM_TYPE , // used only in ColorTheme (and not in source)
  21. TOKEN_ENUM_ELM , // used only in ColorTheme (and not in source)
  22. TOKEN_FUNC , // used only in ColorTheme (and not in source)
  23. TOKEN_SELECT , // used only in ColorTheme (and not in source)
  24. TOKEN_LINE_HIGHLIGHT , // used only in ColorTheme (and not in source)
  25. TOKEN_SYMBOL_HIGHLIGHT , // used only in ColorTheme (and not in source)
  26. TOKEN_BRACE_HIGHLIGHT , // used only in ColorTheme (and not in source)
  27. TOKEN_PREPROC_DISABLED , // used only in ColorTheme (and not in source)
  28. TOKEN_ELM_BACKGROUND , // used only in ColorTheme (and not in source)
  29. TOKEN_ELM_NAME , // used only in ColorTheme (and not in source)
  30. TOKEN_LINE_NUM_BACKGROUND, // used only in ColorTheme (and not in source)
  31. TOKEN_LINE_NUM , // used only in ColorTheme (and not in source)
  32. TOKEN_TYPES ,
  33. };
  34. /******************************************************************************/
  35. STRUCT(Token , BStr)
  36. //{
  37. Bool def_decl, ctor_initializer, macro;
  38. TOKEN_TYPE type;
  39. Int col, // original position (column) in source where the token starts
  40. source_index, // index of the token in source
  41. macro_depth , // helper used in preprocessing
  42. macro_col ; // if this token was created by macro, then this member specifies column in source of that macro
  43. Line *macro_line ; // if this token was created by macro, then this member specifies line in source of that macro
  44. SymbolPtr symbol; // must be Ptr because can refer to a Symbol in different file
  45. Symbol *parent;
  46. Line *line;
  47. Int lineIndex()C; // original position (line) in source where the token starts
  48. VecI2 pos ()C {return VecI2(col, lineIndex());} // original position in source where the token starts
  49. Bool sameMacro(C Token &token)C;
  50. Token& set (C BStr &s, Int col, Line &line, TOKEN_TYPE type);
  51. Token& setCustom (C Str &s, Int col, Line &line, TOKEN_TYPE type);
  52. Token& setBorrowed(CChar *d, Int length, Int col, Line &line, TOKEN_TYPE type);
  53. Token& macroPos (Int col, Line &line) {T.macro_col=col; T.macro_line=&line; return T;}
  54. Token& macroDepth(Int depth ) {T.macro_depth=depth; return T;}
  55. void asText(Str &str)C;
  56. void reset() {symbol=null; parent=null; def_decl=false; ctor_initializer=false;}
  57. Bool save(File &f, StrLibrary &sl, C Str &text)C;
  58. Bool load(File &f, StrLibrary &sl, C Str &text, Line &line, Str &temp);
  59. Token() {reset(); col=0; line=null; source_index=-1; type=TOKEN_NONE; macro=false; macro_col=-1; macro_line=null; macro_depth=0;}
  60. };
  61. /******************************************************************************/
  62. inline Bool ValidType(TOKEN_TYPE type) {return type!=TOKEN_NONE && type!=TOKEN_REMOVE;}
  63. inline Bool TokenType(TOKEN_TYPE type) {return type!=TOKEN_NONE && type!=TOKEN_REMOVE && type!=TOKEN_COMMENT;}
  64. inline Bool TextType(TOKEN_TYPE type) {return type==TOKEN_TEXT8 || type==TOKEN_TEXT16 || type==TOKEN_CHAR8 || type==TOKEN_CHAR16;}
  65. inline Bool ConstType(TOKEN_TYPE type) {return type==TOKEN_TEXT8 || type==TOKEN_TEXT16 || type==TOKEN_CHAR8 || type==TOKEN_CHAR16 || type==TOKEN_NUMBER;}
  66. inline TOKEN_TYPE MainType(TOKEN_TYPE type)
  67. {
  68. switch(type)
  69. {
  70. case TOKEN_CHAR8:
  71. case TOKEN_TEXT8: return TOKEN_TEXT8;
  72. case TOKEN_CHAR16:
  73. case TOKEN_TEXT16: return TOKEN_TEXT16;
  74. case TOKEN_COMMENT: return TOKEN_COMMENT;
  75. default: return TOKEN_CODE;
  76. }
  77. }
  78. inline Bool MustSeparate(TOKEN_TYPE a, TOKEN_TYPE b)
  79. {
  80. return (a==TOKEN_CODE || a==TOKEN_KEYWORD)
  81. && (b==TOKEN_CODE || b==TOKEN_KEYWORD);
  82. }
  83. Bool TextToIDAt (C Str &text, Int pos, UID &id, VecI2 &range); // !! this function is not multi-threaded safe !!
  84. Bool TextToIDInside(C Str &text, Int pos, UID &id, VecI2 &range); // !! this function is not multi-threaded safe !!
  85. /******************************************************************************/
  86. } // namespace
  87. #endif
  88. /******************************************************************************/