CE Token.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Edit{
  5. /******************************************************************************/
  6. // BORROWED STRING
  7. /******************************************************************************/
  8. enum BS_CODES
  9. {
  10. BSC_TMPL_B ,
  11. BSC_TMPL_E ,
  12. BSC_CUSTOM ,
  13. BSC_BORROWED,
  14. };
  15. static void BStrSave(C BStr &t, File &f, C Str &text)
  16. {
  17. if(t==TMPL_B)f.cmpUIntV(BSC_TMPL_B);else
  18. if(t==TMPL_E)f.cmpUIntV(BSC_TMPL_E);else
  19. {
  20. UIntPtr offset =t()-text();
  21. Bool invalid=(t.is() && (offset>=text.length() || t.length()>text.length()));
  22. if(t.custom() && t.length() || invalid)f.cmpUIntV(BSC_CUSTOM).putStr(t.asStr());else
  23. {
  24. f.cmpUIntV(BSC_BORROWED+t.length()); if(t.is())f.cmpUIntV(offset);
  25. }
  26. }
  27. }
  28. static void BStrLoad(BStr &t, File &f, C Str &text, Str &temp)
  29. {
  30. t.del();
  31. Int code=f.decUIntV();
  32. if(code==BSC_TMPL_B ) t.setBorrowed(TMPL_B); else
  33. if(code==BSC_TMPL_E ) t.setBorrowed(TMPL_E); else
  34. if(code==BSC_CUSTOM ){f.getStr(temp); t.setCustom (temp );}else
  35. if(code>=BSC_BORROWED)
  36. {
  37. Int length=code-BSC_BORROWED;
  38. if( length && length<=text.length())t.setBorrowed(text()+f.decUIntV(), length);
  39. }
  40. }
  41. /******************************************************************************/
  42. // TOKEN
  43. /******************************************************************************/
  44. Int Token::lineIndex()C {return line ? line->line : -1;}
  45. Bool Token::sameMacro(C Token &token)C {return macro_line==token.macro_line && macro_col==token.macro_col;}
  46. Token& Token::set (C BStr &s, Int col, Line &line, TOKEN_TYPE type) {super::set (s ); T.col=col; T.line=&line; T.type=type; return T;}
  47. Token& Token::setCustom (C Str &s, Int col, Line &line, TOKEN_TYPE type) {super::setCustom (s ); T.col=col; T.line=&line; T.type=type; return T;}
  48. Token& Token::setBorrowed(CChar *d, Int length, Int col, Line &line, TOKEN_TYPE type) {super::setBorrowed(d, length); T.col=col; T.line=&line; T.type=type; return T;}
  49. /******************************************************************************/
  50. void Token::asText(Str &str)C
  51. {
  52. if(T==TMPL_B)str="<";else
  53. if(T==TMPL_E)str=">";else
  54. str=T;
  55. }
  56. enum
  57. {
  58. DEF_DECL=1<<0,
  59. CTOR =1<<1,
  60. MACRO =1<<2,
  61. };
  62. Bool Token::save(File &f, StrLibrary &sl, C Str &text)C
  63. {
  64. BStrSave(T, f, text);
  65. f.putMulti(Byte((def_decl ? DEF_DECL : 0) | (ctor_initializer ? CTOR : 0) | (macro ? MACRO : 0)), type);
  66. f.cmpUIntV(col); sl.putStr(f, symbol.name()); sl.putStr(f, Symbols.name(parent));
  67. return f.ok();
  68. }
  69. Bool Token::load(File &f, StrLibrary &sl, C Str &text, Line &line, Str &temp) // load this after loading source symbols because of 'parent', and line text (Str) because of 'BStr'
  70. {
  71. BStrLoad(T, f, text, temp);
  72. Byte flag; f.getMulti(flag, type); def_decl=FlagTest(flag, DEF_DECL); ctor_initializer=FlagTest(flag, CTOR); macro=FlagTest(flag, MACRO);
  73. col=f.decUIntV(); sl.getStr(f, temp); symbol=temp; sl.getStr(f, temp); parent=SymbolPtr().get(temp)(); T.line=&line; macro_col=-1; macro_line=null; macro_depth=0;
  74. if(f.ok())return true;
  75. del(); return false;
  76. }
  77. /******************************************************************************/
  78. // MAIN
  79. /******************************************************************************/
  80. static Str TempStr;
  81. Bool TextToIDAt(C Str &text, Int pos, UID &id, VecI2 &range)
  82. {
  83. id.zero();
  84. range=pos;
  85. if(text[pos]=='U' && text[pos+1]=='I' && text[pos+2]=='D' && CodeCharType(text[pos-1])!=CHART_CHAR) // UID(..)
  86. {
  87. CalcValue cv[4];
  88. if(CChar *next=_SkipWhiteChars(text()+pos+3 )) if(*next++=='(')
  89. if( next=_SkipWhiteChars(TextValue(next, cv[0])))if(cv[0].type)if(*next++==',')
  90. if( next=_SkipWhiteChars(TextValue(next, cv[1])))if(cv[1].type)if(*next++==',')
  91. if( next=_SkipWhiteChars(TextValue(next, cv[2])))if(cv[2].type)if(*next++==',')
  92. if( next=_SkipWhiteChars(TextValue(next, cv[3])))if(cv[3].type)if(*next ==')')
  93. {
  94. REPAO(id.i)=cv[i].asUInt();
  95. range.set(pos, next-text());
  96. return true;
  97. }
  98. }else
  99. if(text[pos]=='"' && text[pos+24+1]=='"') // ".." 24 char ID
  100. {
  101. TempStr.clear(); for(Int c=pos+1, j=c; j<c+24; j++)TempStr+=text[j]; // operate on 'TempStr' so it doesn't require memory allocation all the time
  102. if(DecodeFileName(TempStr, id))
  103. {
  104. range.set(pos, pos+24+1);
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. Bool TextToIDInside(C Str &text, Int pos, UID &id, VecI2 &range)
  111. {
  112. REP(pos+1)if(TextToIDAt(text, i, id, range)) // start from pos and go back
  113. {
  114. if(pos<=range.y && CE.cei().idToText(id).is())return true; // if cursor within range and it's a text ID
  115. break; // pause on first ID found
  116. }
  117. id.zero(); return false;
  118. }
  119. /******************************************************************************/
  120. }}
  121. /******************************************************************************/