NodeBase.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.Debugging;
  6. using MoonSharp.Interpreter.Execution;
  7. using MoonSharp.Interpreter.Execution.VM;
  8. namespace MoonSharp.Interpreter.Tree
  9. {
  10. abstract class NodeBase
  11. {
  12. public Script Script { get; private set; }
  13. protected ScriptLoadingContext LoadingContext { get; private set; }
  14. public NodeBase(ScriptLoadingContext lcontext)
  15. {
  16. Script = lcontext.Script;
  17. }
  18. public abstract void Compile(ByteCode bc);
  19. //protected SourceRef BuildSourceRef(IToken token, ITerminalNode terminalNode)
  20. //{
  21. // return RegisterSourceRef(new SourceRef(LoadingContext.Source.SourceID, token.Column, token.Column + terminalNode.GetText().Length, token.Line, token.Line, true));
  22. //}
  23. //protected SourceRef BuildSourceRef(IToken token1, IToken token2 = null)
  24. //{
  25. // token2 = token2 ?? token1;
  26. // return RegisterSourceRef(new SourceRef(LoadingContext.Source.SourceID, token1.Column, token2.Column + token2.Text.Length, token1.Line, token2.Line, true));
  27. //}
  28. //protected SourceRef BuildSourceRef(ITerminalNode terminalNode)
  29. //{
  30. // return BuildSourceRef(terminalNode.Symbol, terminalNode);
  31. //}
  32. private SourceRef RegisterSourceRef(SourceRef sourceRef)
  33. {
  34. LoadingContext.Source.Refs.Add(sourceRef);
  35. sourceRef.Type = this.GetType().Name;
  36. return sourceRef;
  37. }
  38. protected static Token CheckTokenType(ScriptLoadingContext lcontext, TokenType tokenType)
  39. {
  40. Token t = lcontext.Lexer.Current;
  41. if (t.Type != tokenType)
  42. throw new SyntaxErrorException(t, "unexpected symbol near '{0}'", t.Text);
  43. lcontext.Lexer.Next();
  44. return t;
  45. }
  46. protected static Token CheckTokenType(ScriptLoadingContext lcontext, TokenType tokenType1, TokenType tokenType2)
  47. {
  48. Token t = lcontext.Lexer.Current;
  49. if (t.Type != tokenType1 && t.Type != tokenType2)
  50. throw new SyntaxErrorException(t, "unexpected symbol near '{0}'", t.Text);
  51. lcontext.Lexer.Next();
  52. return t;
  53. }
  54. protected static Token CheckTokenType(ScriptLoadingContext lcontext, TokenType tokenType1, TokenType tokenType2, TokenType tokenType3)
  55. {
  56. Token t = lcontext.Lexer.Current;
  57. if (t.Type != tokenType1 && t.Type != tokenType2 && t.Type != tokenType3)
  58. throw new SyntaxErrorException(t, "unexpected symbol near '{0}'", t.Text);
  59. lcontext.Lexer.Next();
  60. return t;
  61. }
  62. protected static void CheckTokenTypeNotNext(ScriptLoadingContext lcontext, TokenType tokenType)
  63. {
  64. Token t = lcontext.Lexer.Current;
  65. if (t.Type != tokenType)
  66. throw new SyntaxErrorException(t, "unexpected symbol near '{0}'", t.Text);
  67. }
  68. protected static void CheckMatch(ScriptLoadingContext lcontext, Token originalToken, TokenType expectedTokenType, string expectedTokenText)
  69. {
  70. if (lcontext.Lexer.Current.Type != expectedTokenType)
  71. throw new SyntaxErrorException(lcontext.Lexer.Current,
  72. "'{0}' expected (to close '{1}' at line {2}) near '{3}'",
  73. expectedTokenText, originalToken.Text, originalToken.FromLine, lcontext.Lexer.Current.Text);
  74. lcontext.Lexer.Next();
  75. }
  76. }
  77. }