private int GetPriority(TokenExpression token) { switch (token) { case TokenExpression.LogicalOr: return(0); case TokenExpression.LogicalAnd: return(1); case TokenExpression.Equal: case TokenExpression.UnEqual: return(2); case TokenExpression.Great: case TokenExpression.Less: case TokenExpression.GreatEqual: case TokenExpression.LessEqual: return(3); case TokenExpression.Add: case TokenExpression.Sub: return(4); case TokenExpression.Div: case TokenExpression.Mul: return(5); case TokenExpression.LogicalNot: return(6); default: throw new Exception("unexpected token"); } }
private Operation GetOperation(TokenExpression token) { switch (token) { case TokenExpression.Add: return(Operation.Add); case TokenExpression.Sub: return(Operation.Sub); case TokenExpression.Mul: return(Operation.Mul); case TokenExpression.Div: return(Operation.Div); case TokenExpression.Great: return(Operation.Great); case TokenExpression.Less: return(Operation.Less); case TokenExpression.Equal: return(Operation.Equal); case TokenExpression.UnEqual: return(Operation.UnEqual); case TokenExpression.LogicalAnd: return(Operation.LogicalAnd); case TokenExpression.LogicalOr: return(Operation.LogicalOr); case TokenExpression.LogicalNot: return(Operation.LogicalNot); } throw new Exception("unexpected token"); }
public NLPActionResult UserEnteredANumericExpression(TokenExpression tne) { if (tne.Type == typeof(double)) { var result = tne.ToDouble(); if (result != null) { st.Say("Hmmm .. a puzzle, I love puzzles..."); } Thread.Sleep(250); st.Say("..."); Thread.Sleep(250); st.Say("I think the answer is " + result); } else if (tne is TemporalSet) { throw new System.Exception("Check the relative priority of this rule and UserEnteredATemporalExpression"); } else { st.Say($"You entered an expression {tne.Describe(true)}"); st.Say($"You can serialize a token expression {tne.Serialize()}"); st.Say($"You can get the unbound variables on it {tne.UnboundVariables}"); st.Say($"To create variables, add a TokenExpressionVariableAcces to the Lexeme store using the words you want."); st.Say($"You can evaluate a TokenExpression in the context of an environment containing variable values."); st.Say($"You can also convert it to a LINQ Expression or to a SQL Query."); } return(NLPActionResult.None); }
[Priority(500, probability: 0.5)] // lower probability public NLPActionResult UserEnteredANumericExpression(TokenExpression tne) { if (tne.Type == typeof(double)) { var result = tne.ToDouble(); if (result != null) { st.Say("Hmmm .. a puzzle, I love puzzles..."); } Thread.Sleep(250); st.Say("..."); Thread.Sleep(250); st.Say("I think the answer is " + result); } else if (tne is TemporalSet) { // The other rule should have fired first because it has a higher probability return(NLPActionResult.Continue); } else { st.Say($"You entered an expression {tne.Describe(true)}"); st.Say($"You can serialize a token expression {tne.Serialize()}"); st.Say($"You can get the unbound variables on it {string.Join(",", tne.UnboundVariables)}"); st.Say($"To create variables, add a TokenExpressionVariableAcces to the Lexeme store using the words you want."); st.Say($"You can evaluate a TokenExpression in the context of an environment containing variable values."); st.Say($"You can also convert it to a LINQ Expression or to a SQL Query."); } return(NLPActionResult.None); }
public override void Visit(TokenExpression x) { if (x.Token == DTokens.Incomplete) { LastCallExpression = peek; } }
public UserRule(string name, string rulePattern, TokenExpression tokenConvertionPattern) { this.Priority = RulePriority.UserRule; this.Name = name; this.RulePattern = rulePattern; this.TokenConvertionPattern = tokenConvertionPattern; }
private bool IsBinaryOperation(TokenExpression token) { return(token == TokenExpression.Add || token == TokenExpression.Sub || token == TokenExpression.Mul || token == TokenExpression.Div || token == TokenExpression.Great || token == TokenExpression.Less || token == TokenExpression.Equal || token == TokenExpression.UnEqual || token == TokenExpression.LogicalAnd || token == TokenExpression.LogicalOr || token == TokenExpression.GreatEqual || token == TokenExpression.LessEqual); }
/// <summary> /// Executes the token expression action /// </summary> /// <param name="expression">a token expression</param> private void ExecuteExpression(TokenExpression expression) { if (!canMoveHandler()) { pendantActions.Enqueue(expression); return; } switch (expression.Token) { case CommandToken.Move: MoveCommand(expression.Operands); break; case CommandToken.Turn: RotateCommand(expression.Operands); break; case CommandToken.Stop: haltHandler(); break; case CommandToken.Beep: beepHandler(); break; case CommandToken.Signal: bool isOn = expression.Operands[0].Equals("on"); bool isOff = expression.Operands[0].Equals("off"); bool turnOn = isOn && isOff == false; if (!isOn && !isOff) { break; } signalHandler(turnOn); break; case CommandToken.Marker: bool isDown = expression.Operands[0].Equals("down"); bool isUp = expression.Operands[0].Equals("up"); bool goDown = isDown && isUp == false; if (!isDown && !isUp) { break; } markerHandler(goDown); break; default: break; } }
public static AbstractType[] GetResolvedConstructorOverloads(TokenExpression tk, ResolverContextStack ctxt) { if (tk.Token == DTokens.This || tk.Token == DTokens.Super) { var classRef = EvaluateType(tk, ctxt) as TemplateIntermediateType; if (classRef != null) return D_Parser.Resolver.TypeResolution.TypeDeclarationResolver.HandleNodeMatches(GetConstructors(classRef), ctxt, classRef, tk); } return null; }
private static void ResolveTokenExpression(this ITokenExpression expression, IOilexerGrammarTokenEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors) { IList<ITokenItem> rCopy = (from item in expression select item).ToList(); TokenExpression te = expression as TokenExpression; IEnumerable<ITokenItem> finalVersion = from item in rCopy select (item.ResolveTokenExpressionItem(entry, file, errors)); te.BaseCollection.Clear(); foreach (ITokenItem iti in finalVersion) te.BaseCollection.Add(iti); }
public static AbstractType[] GetResolvedConstructorOverloads(TokenExpression tk, ResolutionContext ctxt) { if (tk.Token == DTokens.This || tk.Token == DTokens.Super) { var classRef = EvaluateType(tk, ctxt) as TemplateIntermediateType; if (classRef != null) { return(DSharp.Resolver.TypeResolution.TypeDeclarationResolver.HandleNodeMatches(GetConstructors(classRef), ctxt, classRef, tk)); } } return(null); }
public override void Visit(TokenExpression e) { if (e.Token == DTokens.Incomplete) { halt = true; const MemberFilter BaseAsmFlags = MemberFilter.Classes | MemberFilter.StructsAndUnions | MemberFilter.Enums | MemberFilter.Methods | MemberFilter.TypeParameters | MemberFilter.Types | MemberFilter.Variables; if (scopedStatement is AsmStatement || scopedStatement is AsmStatement.InstructionStatement) { prv = new CtrlSpaceCompletionProvider(cdgen, scopedBlock, BaseAsmFlags | MemberFilter.x86Registers | MemberFilter.x64Registers | MemberFilter.Labels); } else if (scopedStatement is AsmStatement.RawDataStatement) { prv = new CtrlSpaceCompletionProvider(cdgen, scopedBlock, BaseAsmFlags | MemberFilter.Labels); } else /*if (handlesInitializer)*/ // Why only in initializers? { prv = new CtrlSpaceCompletionProvider(cdgen, scopedBlock, shownKeywords.Count == 0 ? MemberFilter.All | MemberFilter.ExpressionKeywords : shownKeywords.Peek()); } } }
/// <summary> /// Enqueue a list of commands and execute them when a token End is received /// </summary> public void Execute(string rawCommand) { try { var expression = new TokenExpression(rawCommand); if (expression.Token.Equals(CommandToken.End)) { while (!commandQueue.IsNullOrEmpty()) { var queuedExpression = commandQueue.Dequeue(); ExecuteExpression(queuedExpression); } commandQueue.Clear(); } else { commandQueue.Enqueue(expression); } } catch (Exception) { } }
public void TestThatSimpleRuleWorkCorrect() { // Arrange var text = "int a;"; var name = "name"; var stream = new MemoryStream().FromString(text); var tokenPatter = new TokenExpression(name, new Dictionary <string, int> { { "type", 0 }, { "name", 2 } }); var rule = new UserRule(name: name, rulePattern: this.rawRule, tokenConvertionPattern: tokenPatter); // Act rule.Check(stream); var result = rule.Convert(); // Assert Assert.AreEqual(result.Name, name); Assert.IsNull(result.Value); Assert.AreEqual(2, result.Childs.Count); var type = result.Childs.FirstOrDefault(x => x.Name == "type"); Assert.IsNotNull(type); Assert.AreEqual(0, type.Childs.Count); Assert.AreEqual("int", type.Value); var vrName = result.Childs.FirstOrDefault(x => x.Name == "name"); Assert.IsNotNull(vrName); Assert.AreEqual(0, vrName.Childs.Count); Assert.AreEqual("a", vrName.Value); }
public TokenClass(TokenExpression parent) { this.parent = parent; }
public static object CreateExpression(SDFState state, string name, Hashtable arguments) { TokenExpression o = new TokenExpression(); ((TokenStringRegistry) state[typeof(TokenStringRegistry)]).AddObject(arguments["name"].ToString(), o.Token); return o; }
IExpression PrimaryExpression(IBlockNode Scope=null) { bool isModuleScoped = laKind == Dot; if (isModuleScoped) { Step(); if (IsEOF) { var dot = new TokenExpression(Dot) { Location = t.Location, EndLocation = t.EndLocation }; return new PostfixExpression_Access{ PostfixForeExpression = dot, AccessExpression = new TokenExpression(DTokens.Incomplete) }; } } // TemplateInstance if (IsTemplateInstance) { var tix = TemplateInstance(Scope); if (tix != null) tix.ModuleScopedIdentifier = isModuleScoped; return tix; } if (IsLambaExpression()) return LambaExpression(Scope); CodeLocation startLoc; switch (laKind) { // ArrayLiteral | AssocArrayLiteral case OpenSquareBracket: return ArrayLiteral(Scope); case New: return NewExpression(Scope); case Typeof: return new TypeDeclarationExpression(TypeOf()); case __traits: return TraitsExpression(); // Dollar (== Array length expression) case Dollar: Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; case Identifier: Step(); return new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation, ModuleScoped = isModuleScoped }; // SpecialTokens (this,super,null,true,false,$) // $ has been handled before case This: case Super: case Null: case True: case False: Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; case OpenParenthesis: if (IsFunctionLiteral()) goto case Function; // ( Expression ) Step(); var ret = new SurroundingParenthesesExpression() {Location=t.Location }; ret.Expression = Expression(); Expect(CloseParenthesis); ret.EndLocation = t.EndLocation; return ret; case Literal: Step(); startLoc = t.Location; // Concatenate multiple string literals here if (t.LiteralFormat == LiteralFormat.StringLiteral || t.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { var sb = new StringBuilder(t.RawCodeRepresentation ?? t.Value); while (la.LiteralFormat == LiteralFormat.StringLiteral || la.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { Step(); sb.Append(t.RawCodeRepresentation ?? t.Value); } return new IdentifierExpression(sb.ToString(), t.LiteralFormat, t.Subformat) { Location = startLoc, EndLocation = t.EndLocation }; } //else if (t.LiteralFormat == LiteralFormat.CharLiteral)return new IdentifierExpression(t.LiteralValue) { LiteralFormat=t.LiteralFormat,Location = startLoc, EndLocation = t.EndLocation }; return new IdentifierExpression(t.LiteralValue, t.LiteralFormat, t.Subformat) { Location = startLoc, EndLocation = t.EndLocation }; // FunctionLiteral case Delegate: case Function: case OpenCurlyBrace: var fl = new FunctionLiteral() { Location=la.Location}; fl.AnonymousMethod.Location = la.Location; if (laKind == Delegate || laKind == Function) { Step(); fl.LiteralToken = t.Kind; } // file.d:1248 /* listdir (".", delegate bool (DirEntry * de) { auto s = std.string.format("%s : c %s, w %s, a %s", de.name, toUTCString (de.creationTime), toUTCString (de.lastWriteTime), toUTCString (de.lastAccessTime)); return true; } ); */ if (laKind != OpenCurlyBrace) // foo( 1, {bar();} ); -> is a legal delegate { if (!IsFunctionAttribute && Lexer.CurrentPeekToken.Kind == OpenParenthesis) fl.AnonymousMethod.Type = BasicType(); else if (laKind != OpenParenthesis && laKind != OpenCurlyBrace) fl.AnonymousMethod.Type = Type(); if (laKind == OpenParenthesis) fl.AnonymousMethod.Parameters = Parameters(fl.AnonymousMethod); FunctionAttributes(fl.AnonymousMethod); } if (!IsEOF) { FunctionBody(fl.AnonymousMethod); fl.EndLocation = fl.AnonymousMethod.EndLocation; } else fl.EndLocation = la.Location; if (Scope != null) Scope.Add(fl.AnonymousMethod); return fl; // AssertExpression case Assert: Step(); startLoc = t.Location; Expect(OpenParenthesis); var ce = new AssertExpression() { Location=startLoc}; var exprs = new List<IExpression>(); exprs.Add(AssignExpression()); if (laKind == (Comma)) { Step(); exprs.Add(AssignExpression()); } ce.AssignExpressions = exprs.ToArray(); Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; // MixinExpression case Mixin: Step(); var me = new MixinExpression() { Location=t.Location}; if (Expect(OpenParenthesis)) { me.AssignExpression = AssignExpression(); Expect(CloseParenthesis); } me.EndLocation = t.EndLocation; return me; // ImportExpression case Import: Step(); var ie = new ImportExpression() { Location=t.Location}; Expect(OpenParenthesis); ie.AssignExpression = AssignExpression(); Expect(CloseParenthesis); ie.EndLocation = t.EndLocation; return ie; // TypeidExpression case Typeid: Step(); var tide = new TypeidExpression() { Location=t.Location}; Expect(OpenParenthesis); if (IsAssignExpression()) tide.Expression = AssignExpression(Scope); else { Lexer.PushLookAheadBackup(); AllowWeakTypeParsing = true; tide.Type = Type(); AllowWeakTypeParsing = false; if (tide.Type == null || laKind != CloseParenthesis) { Lexer.RestoreLookAheadBackup(); tide.Expression = AssignExpression(); } else Lexer.PopLookAheadBackup(); } Expect (CloseParenthesis); tide.EndLocation = t.EndLocation; return tide; // IsExpression case Is: Step(); var ise = new IsExpression() { Location = t.Location }; Expect(OpenParenthesis); if ((ise.TestedType = Type()) == null) SynErr(laKind, "In an IsExpression, either a type or an expression is required!"); if (ise.TestedType != null) { if (laKind == Identifier && (Lexer.CurrentPeekToken.Kind == CloseParenthesis || Lexer.CurrentPeekToken.Kind == Equal || Lexer.CurrentPeekToken.Kind == Colon)) { Step(); Strings.Add(strVal); ise.TypeAliasIdentifierHash = strVal.GetHashCode(); ise.TypeAliasIdLocation = t.Location; } else if (IsEOF) ise.TypeAliasIdentifierHash = DTokens.IncompleteIdHash; } if (laKind == Colon || laKind == Equal) { Step(); ise.EqualityTest = t.Kind == Equal; } else if (laKind == CloseParenthesis) { Step(); ise.EndLocation = t.EndLocation; return ise; } /* TypeSpecialization: Type struct union class interface enum function delegate super const immutable inout shared return */ bool specialTest = false; if (ise.EqualityTest) { switch (laKind) { case Typedef: // typedef is possible although it's not yet documented in the syntax docs case Enum: case Delegate: case Function: case Super: case Return: specialTest = true; break; case Const: case Immutable: case InOut: case Shared: specialTest = Peek(1).Kind == CloseParenthesis || Lexer.CurrentPeekToken.Kind == Comma; break; default: specialTest = IsClassLike(laKind); break; } } if (specialTest) { Step(); ise.TypeSpecializationToken = t.Kind; } else ise.TypeSpecialization = Type(); // TemplateParameterList if (laKind == Comma) { var tempParam = new List<TemplateParameter>(); do { Step(); tempParam.Add(TemplateParameter(null)); } while (laKind == Comma); ise.TemplateParameterList = tempParam.ToArray(); } Expect(CloseParenthesis); ise.EndLocation = t.EndLocation; return ise; default: if (DTokens.IsMetaIdentifier(laKind)) goto case Dollar; else if (IsBasicType()) { // BasicType . Identifier startLoc = la.Location; var bt=BasicType(); if ((bt is TypeOfDeclaration || bt is MemberFunctionAttributeDecl) && laKind!=Dot) return new TypeDeclarationExpression(bt); // Things like incomplete 'float.' expressions shall be parseable, too if (Expect(Dot) && (Expect(Identifier) || IsEOF)) return new PostfixExpression_Access() { PostfixForeExpression = new TypeDeclarationExpression(bt), AccessExpression = IsEOF ? new TokenExpression(Incomplete) as IExpression : new IdentifierExpression(t.Value) { Location=t.Location, EndLocation=t.EndLocation }, EndLocation = t.EndLocation }; return null; } SynErr(Identifier); if(laKind != CloseCurlyBrace) Step(); if (IsEOF) return new TokenExpression (DTokens.Incomplete) { Location = t.Location, EndLocation = t.Location }; // Don't know why, in rare situations, t tends to be null.. if (t == null) return null; return new TokenExpression() { Location = t.Location, EndLocation = t.EndLocation }; } }
ISemantic E(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: if (eval && resolveConstOnly) { EvalError(new NoConstException(x)); return null; } var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; if (classDef is DClassLike) return TypeDeclarationResolver.HandleNodeMatch(classDef, ctxt, null, x); /* * TODO: Return an object reference to the 'this' object. */ break; case DTokens.Super: // References super type of currently scoped class declaration if (eval && resolveConstOnly) { EvalError(new NoConstException(x)); return null; } classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; if (classDef != null) { var tr = DResolver.ResolveBaseClasses(new ClassType(classDef as DClassLike, null, null), ctxt, true); if (tr.Base != null) { // Important: Overwrite type decl base with 'super' token tr.Base.DeclarationOrExpressionBase = x; return tr.Base; } } /* * TODO: Return an object reference to 'this', wheras the type is the superior type. */ break; case DTokens.Null: if (eval && resolveConstOnly) { EvalError(new NoConstException(x)); return null; } if (eval) { //TODO } return null; case DTokens.Dollar: if (!eval) return new PrimitiveType (DTokens.Int); // It's only allowed if the evaluation stack contains an array value if (ValueProvider.CurrentArrayLength != -1) return new PrimitiveValue(DTokens.Int, ValueProvider.CurrentArrayLength, x); else { EvalError(x, "Dollar not allowed here!"); return null; } case DTokens.True: if (!eval) return new PrimitiveType (DTokens.Bool); return new PrimitiveValue(DTokens.Bool, 1, x); case DTokens.False: if (!eval) return new PrimitiveType (DTokens.Bool); return new PrimitiveValue(DTokens.Bool, 0, x); case DTokens.__FILE__: if (!eval) return GetStringType(); return new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).FileName); case DTokens.__LINE__: if (!eval) return new PrimitiveType (DTokens.Int); return new PrimitiveValue(DTokens.Int, x.Location.Line, x); case DTokens.__MODULE__: if (!eval) return GetStringType(); return new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).ModuleName); case DTokens.__FUNCTION__: //TODO case DTokens.__PRETTY_FUNCTION__: if (!eval) return GetStringType(); var dm = ctxt.ScopedStatement.ParentNode as DMethod; return new ArrayValue(GetStringType(), dm == null ? "<not inside function>" : dm.ToString(false,true)); } return null; }
IExpression ParseAsmPrimaryExpression(IBlockNode Scope, IStatement Parent) { switch (laKind) { case OpenSquareBracket: Step (); var e = new PostfixExpression_ArrayAccess (ParseAsmExpression (Scope, Parent)); Expect (CloseSquareBracket); e.EndLocation = t.EndLocation; return e; case Dollar: var ins = Parent as AsmStatement.InstructionStatement; if (ins == null || (!ins.IsJmpFamily && ins.Operation != AsmStatement.InstructionStatement.OpCode.call)) SynErr(Dollar, "The $ operator is only valid on jmp and call instructions!"); Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; case Literal: Step(); return new IdentifierExpression(t.LiteralValue, t.LiteralFormat, t.Subformat) { Location = t.Location, EndLocation = t.EndLocation }; case This: Step(); return new TokenExpression(This) { Location = t.Location, EndLocation = t.EndLocation }; // AsmTypePrefix case DTokens.Byte: case DTokens.Short: case DTokens.Int: case DTokens.Float: case DTokens.Double: case DTokens.Real: case __LOCAL_SIZE: Step (); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; case Identifier: Step(); if (AsmRegisterExpression.IsRegister(t.Value)) { string reg = t.Value; if (reg == "ST" && laKind == OpenParenthesis) { reg += "("; Step(); if (Expect(Literal)) { reg += t.LiteralValue.ToString(); if (laKind != CloseParenthesis) SynErr(CloseParenthesis); else Step(); reg += ")"; } } switch (reg) { case "ES": case "CS": case "SS": case "DS": case "GS": case "FS": if (laKind == Colon) { var ex = new AsmRegisterExpression() { Location = t.Location, EndLocation = t.EndLocation, Register = string.Intern(reg) }; Step(); // NOTE: DMD actually allows you to not have an expression after a // segment specifier, however I consider this a bug, and, as // such, am making an expression in that form fail to parse. return new UnaryExpression_SegmentBase() { RegisterExpression = ex, UnaryExpression = ParseAsmExpression(Scope, Parent) }; } goto default; default: // This check is required because of how ST registers are handled. if (AsmRegisterExpression.IsRegister(reg)) return new AsmRegisterExpression() { Location = t.Location, EndLocation = t.EndLocation, Register = string.Intern(reg) }; SynErr(Identifier, "Unknown register!"); return IsEOF ? new TokenExpression(Incomplete) : null; } } else { IExpression outer = new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation }; while (laKind == Dot) { Step(); if (Expect(Identifier)) outer = new PostfixExpression_Access() { AccessExpression = new IdentifierExpression(t.Value), PostfixForeExpression = outer }; else outer = new TokenExpression(Incomplete); Step(); } return outer; } default: SynErr(Identifier, "Expected a $, literal or an identifier!"); Step(); if (IsEOF) return new TokenExpression(Incomplete); return null; } }
public void Visit(TokenExpression x) { }
public AbstractType Visit(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) { classDef = classDef.Parent as IBlockNode; } if (classDef is DClassLike) { var res = TypeDeclarationResolver.HandleNodeMatch(classDef, ctxt, null, x); res.NonStaticAccess = true; return(res); } //TODO: Throw return(null); case DTokens.Super: // References super type of currently scoped class declaration classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) { classDef = classDef.Parent as IBlockNode; } if (classDef is DClassLike) { var tr = DResolver.ResolveClassOrInterface(classDef as DClassLike, ctxt, null, true); if (tr.Base != null) { // Important: Overwrite type decl base with 'super' token tr.Base.DeclarationOrExpressionBase = x; tr.Base.NonStaticAccess = true; return(tr.Base); } } //TODO: Throw return(null); case DTokens.Null: return(null); case DTokens.Dollar: return(new PrimitiveType(DTokens.Int)); // Really integer or every kind of iterator type? case DTokens.False: case DTokens.True: return(new PrimitiveType(DTokens.Bool)); case DTokens.__FILE__: return(GetStringType()); case DTokens.__LINE__: return(new PrimitiveType(DTokens.Int)); case DTokens.__MODULE__: return(GetStringType()); case DTokens.__FUNCTION__: //TODO return(null); case DTokens.__PRETTY_FUNCTION__: return(GetStringType()); default: return(null); } }
IExpression PrimaryExpression(IBlockNode Scope=null) { bool isModuleScoped = laKind == Dot; if (isModuleScoped) { Step(); if (IsEOF) { LastParsedObject = new TokenExpression(Dot) { Location = t.Location, EndLocation = t.EndLocation }; TrackerVariables.ExpectingIdentifier = true; } } // Dollar (== Array length expression) if (laKind == Dollar || DTokens.MetaIdentifiers[laKind]) { Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; } // TemplateInstance if (IsTemplateInstance) { var tix = TemplateInstance(Scope); if (tix != null) tix.ModuleScopedIdentifier = isModuleScoped; return tix; } if (IsLambaExpression()) return LambaExpression(Scope); // Identifier if (laKind == Identifier) { Step(); return new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation, ModuleScoped = isModuleScoped }; } // SpecialTokens (this,super,null,true,false,$) // $ has been handled before if (laKind == (This) || laKind == (Super) || laKind == (Null) || laKind == (True) || laKind == (False)) { Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; } #region Literal if (laKind == Literal) { Step(); var startLoc = t.Location; // Concatenate multiple string literals here if (t.LiteralFormat == LiteralFormat.StringLiteral || t.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { var sb = new StringBuilder(t.RawCodeRepresentation ?? t.Value); while (la.LiteralFormat == LiteralFormat.StringLiteral || la.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { Step(); sb.Append(t.RawCodeRepresentation ?? t.Value); } return new IdentifierExpression(sb.ToString(), t.LiteralFormat, t.Subformat) { Location = startLoc, EndLocation = t.EndLocation }; } //else if (t.LiteralFormat == LiteralFormat.CharLiteral)return new IdentifierExpression(t.LiteralValue) { LiteralFormat=t.LiteralFormat,Location = startLoc, EndLocation = t.EndLocation }; return new IdentifierExpression(t.LiteralValue, t.LiteralFormat, t.Subformat) { Location = startLoc, EndLocation = t.EndLocation }; } #endregion #region ArrayLiteral | AssocArrayLiteral if (laKind == (OpenSquareBracket)) { Step(); var startLoc = t.Location; // Empty array literal if (laKind == CloseSquareBracket) { Step(); return new ArrayLiteralExpression(null) {Location=startLoc, EndLocation = t.EndLocation }; } /* * If it's an initializer, allow NonVoidInitializers as values. * Normal AssignExpressions otherwise. */ bool isInitializer = TrackerVariables.IsParsingInitializer; var firstExpression = isInitializer? NonVoidInitializer(Scope) : AssignExpression(Scope); // Associtative array if (laKind == Colon) { Step(); var ae = isInitializer ? new ArrayInitializer { Location = startLoc } : new AssocArrayExpression { Location=startLoc }; LastParsedObject = ae; var firstValueExpression = isInitializer? NonVoidInitializer(Scope) : AssignExpression(); ae.Elements.Add(new KeyValuePair<IExpression,IExpression>(firstExpression, firstValueExpression)); while (laKind == Comma) { Step(); if (laKind == CloseSquareBracket) break; var keyExpr = AssignExpression(); var valExpr=Expect(Colon) ? (isInitializer? NonVoidInitializer(Scope) : AssignExpression(Scope)) : null; ae.Elements.Add(new KeyValuePair<IExpression,IExpression>(keyExpr,valExpr)); } Expect(CloseSquareBracket); ae.EndLocation = t.EndLocation; return ae; } else // Normal array literal { var ae = new List<IExpression>(); LastParsedObject = ae; ae.Add(firstExpression); while (laKind == Comma) { Step(); if (laKind == CloseSquareBracket) // And again, empty expressions are allowed break; ae.Add(isInitializer? NonVoidInitializer(Scope) : AssignExpression(Scope)); } Expect(CloseSquareBracket); return new ArrayLiteralExpression(ae){ Location=startLoc, EndLocation = t.EndLocation }; } } #endregion #region FunctionLiteral if (laKind == Delegate || laKind == Function || laKind == OpenCurlyBrace || (laKind == OpenParenthesis && IsFunctionLiteral())) { var fl = new FunctionLiteral() { Location=la.Location}; LastParsedObject = fl; fl.AnonymousMethod.Location = la.Location; if (laKind == Delegate || laKind == Function) { Step(); fl.LiteralToken = t.Kind; } // file.d:1248 /* listdir (".", delegate bool (DirEntry * de) { auto s = std.string.format("%s : c %s, w %s, a %s", de.name, toUTCString (de.creationTime), toUTCString (de.lastWriteTime), toUTCString (de.lastAccessTime)); return true; } ); */ if (laKind != OpenCurlyBrace) // foo( 1, {bar();} ); -> is a legal delegate { if (!IsFunctionAttribute && Lexer.CurrentPeekToken.Kind == OpenParenthesis) fl.AnonymousMethod.Type = BasicType(); else if (laKind != OpenParenthesis && laKind != OpenCurlyBrace) fl.AnonymousMethod.Type = Type(); if (laKind == OpenParenthesis) fl.AnonymousMethod.Parameters = Parameters(fl.AnonymousMethod); FunctionAttributes(fl.AnonymousMethod); } if(!IsEOF) FunctionBody(fl.AnonymousMethod); fl.EndLocation = t.EndLocation; if (Scope != null) Scope.Add(fl.AnonymousMethod); return fl; } #endregion #region AssertExpression if (laKind == (Assert)) { Step(); var startLoc = t.Location; Expect(OpenParenthesis); var ce = new AssertExpression() { Location=startLoc}; LastParsedObject = ce; var exprs = new List<IExpression>(); exprs.Add(AssignExpression()); if (laKind == (Comma)) { Step(); exprs.Add(AssignExpression()); } ce.AssignExpressions = exprs.ToArray(); Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; } #endregion #region MixinExpression | ImportExpression if (laKind == Mixin) { Step(); var e = new MixinExpression() { Location=t.Location}; LastParsedObject = e; if (Expect(OpenParenthesis)) { e.AssignExpression = AssignExpression(); Expect(CloseParenthesis); } e.EndLocation = t.EndLocation; return e; } if (laKind == Import) { Step(); var e = new ImportExpression() { Location=t.Location}; LastParsedObject = e; Expect(OpenParenthesis); e.AssignExpression = AssignExpression(); Expect(CloseParenthesis); e.EndLocation = t.EndLocation; return e; } #endregion if (laKind == (Typeof)) { return new TypeDeclarationExpression(TypeOf()); } // TypeidExpression if (laKind == (Typeid)) { Step(); var ce = new TypeidExpression() { Location=t.Location}; LastParsedObject = ce; Expect(OpenParenthesis); if (IsAssignExpression()) ce.Expression = AssignExpression(Scope); else { Lexer.PushLookAheadBackup(); AllowWeakTypeParsing = true; ce.Type = Type(); AllowWeakTypeParsing = false; if (ce.Type == null || laKind != CloseParenthesis) { Lexer.RestoreLookAheadBackup(); ce.Expression = AssignExpression(); } else Lexer.PopLookAheadBackup(); } if (!Expect(CloseParenthesis) && IsEOF) LastParsedObject = (ISyntaxRegion)ce.Type ?? ce.Expression; ce.EndLocation = t.EndLocation; return ce; } #region IsExpression if (laKind == Is) { Step(); var ce = new IsExpression() { Location=t.Location}; LastParsedObject = ce; Expect(OpenParenthesis); if((ce.TestedType = Type())==null) SynErr(laKind, "In an IsExpression, either a type or an expression is required!"); if (ce.TestedType!=null && laKind == Identifier && (Lexer.CurrentPeekToken.Kind == CloseParenthesis || Lexer.CurrentPeekToken.Kind == Equal || Lexer.CurrentPeekToken.Kind == Colon)) { Step(); Strings.Add(strVal); ce.TypeAliasIdentifierHash = strVal.GetHashCode(); ce.TypeAliasIdLocation = t.Location; } if (laKind == CloseParenthesis) { Step(); ce.EndLocation = t.EndLocation; return ce; } if (laKind == Colon || laKind == Equal) { Step(); ce.EqualityTest = t.Kind == Equal; } else if (laKind == CloseParenthesis) { Step(); ce.EndLocation = t.EndLocation; return ce; } /* TypeSpecialization: Type struct union class interface enum function delegate super const immutable inout shared return */ if (ce.EqualityTest && (ClassLike[laKind] || laKind==Typedef || // typedef is possible although it's not yet documented in the syntax docs laKind==Enum || laKind==Delegate || laKind==Function || laKind==Super || laKind==Return || ((laKind==Const || laKind == Immutable || laKind == InOut || laKind == Shared) && (Peek(1).Kind==CloseParenthesis || Lexer.CurrentPeekToken.Kind==Comma)))) { Step(); ce.TypeSpecializationToken = t.Kind; } else ce.TypeSpecialization = Type(); // TemplateParameterList if (laKind == Comma) { var ret = new List<TemplateParameter>(); do { Step(); ret.Add(TemplateParameter(null)); } while (laKind == Comma); ce.TemplateParameterList = ret.ToArray(); } Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; } #endregion // NewExpression if (laKind == (New)) return NewExpression(Scope); // ( Expression ) if (laKind == OpenParenthesis) { Step(); var ret = new SurroundingParenthesesExpression() {Location=t.Location }; LastParsedObject = ret; ret.Expression = Expression(); Expect(CloseParenthesis); ret.EndLocation = t.EndLocation; return ret; } // TraitsExpression if (laKind == (__traits)) return TraitsExpression(); #region BasicType . Identifier if (IsBasicType()) { var startLoc = la.Location; var bt=BasicType(); if ((bt is TypeOfDeclaration || bt is MemberFunctionAttributeDecl) && laKind!=Dot) return new TypeDeclarationExpression(bt); // Things like incomplete 'float.' expressions shall be parseable, too if (Expect(Dot) && (Expect(Identifier) || IsEOF)) return new PostfixExpression_Access() { PostfixForeExpression = new TypeDeclarationExpression(bt), AccessExpression = string.IsNullOrEmpty(t.Value) ? null : new IdentifierExpression(t.Value) { Location=t.Location, EndLocation=t.EndLocation }, EndLocation = t.EndLocation }; return null; } #endregion SynErr(Identifier); if(laKind != CloseCurlyBrace) Step(); // Don't know why, in rare situations, t tends to be null.. if (t == null) return null; return new TokenExpression() { Location = t.Location, EndLocation = t.EndLocation }; }
public TemplateInstanceExpression TemplateInstance(IBlockNode Scope) { var loc = la.Location; var mod = INVALID; if (DTokens.StorageClass [laKind]) { mod = laKind; Step (); } if (!Expect (Identifier)) return null; ITypeDeclaration td = new IdentifierDeclaration (t.Value) { Location = t.Location, EndLocation = t.EndLocation }; td = new TemplateInstanceExpression(mod != DTokens.INVALID ? new MemberFunctionAttributeDecl(mod) { InnerType = td } : td) { Location = loc }; LastParsedObject = td; var args = new List<IExpression>(); if (!Expect(Not)) return td as TemplateInstanceExpression; if (laKind == (OpenParenthesis)) { Step(); if (laKind != CloseParenthesis) { bool init = true; while (laKind == Comma || init) { if (!init) Step(); init = false; if (laKind == CloseParenthesis) break; if (IsEOF) { args.Add(new TokenExpression(DTokens.INVALID) { Location= la.Location, EndLocation=la.EndLocation }); break; } Lexer.PushLookAheadBackup(); bool wp = AllowWeakTypeParsing; AllowWeakTypeParsing = true; var typeArg = Type(); AllowWeakTypeParsing = wp; if (typeArg != null && (laKind == CloseParenthesis || laKind==Comma)){ Lexer.PopLookAheadBackup(); args.Add(new TypeDeclarationExpression(typeArg)); }else { Lexer.RestoreLookAheadBackup(); args.Add(AssignExpression(Scope)); } } } Expect(CloseParenthesis); } else { Step(); /* * TemplateSingleArgument: * Identifier * BasicTypeX * CharacterLiteral * StringLiteral * IntegerLiteral * FloatLiteral * true * false * null * __FILE__ * __LINE__ */ IExpression arg= null; if (t.Kind == Literal) arg = new IdentifierExpression(t.LiteralFormat == LiteralFormat.StringLiteral || t.LiteralFormat == LiteralFormat.VerbatimStringLiteral ? t.Value : t.LiteralValue, t.LiteralFormat, t.Subformat) { Location = t.Location, EndLocation = t.EndLocation }; else if (t.Kind == Identifier) arg = new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation }; else if (BasicTypes[t.Kind]) arg = new TypeDeclarationExpression(new DTokenDeclaration(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }); else if ( t.Kind == True || t.Kind == False || t.Kind == Null || t.Kind == __FILE__ || t.Kind == __LINE__) arg = new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; else if (IsEOF) { TrackerVariables.ExpectingIdentifier = false; td.EndLocation = CodeLocation.Empty; return td as TemplateInstanceExpression; } args.Add(arg); } (td as TemplateInstanceExpression).Arguments = args.ToArray(); td.EndLocation = t.EndLocation; return td as TemplateInstanceExpression; }
public override void Visit(TokenExpression x) { if (x.Token == DTokens.Incomplete) LastCallExpression = peek; }
IExpression PrimaryExpression(IBlockNode Scope=null) { bool isModuleScoped = false; // For minimizing possible overhead, skip 'useless' tokens like an initial dot <<< TODO if (isModuleScoped= laKind == Dot) Step(); if (laKind == __FILE__ || laKind == __LINE__) { Step(); object id = null; if (t.Kind == __FILE__ && doc != null) id = doc.FileName; else if(t.Kind==__LINE__) id = t.line; return new IdentifierExpression(id) { Location=t.Location, EndLocation=t.EndLocation }; } // Dollar (== Array length expression) if (laKind == Dollar) { Step(); return new TokenExpression(laKind) { Location = t.Location, EndLocation = t.EndLocation }; } // TemplateInstance if (laKind == (Identifier) && Lexer.CurrentPeekToken.Kind == (Not) && (Peek().Kind != Is && Lexer.CurrentPeekToken.Kind != In) /* Very important: The 'template' could be a '!is'/'!in' expression - With two tokens each! */) return TemplateInstance(); // Identifier if (laKind == (Identifier)) { Step(); return new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation }; } // SpecialTokens (this,super,null,true,false,$) // $ has been handled before if (laKind == (This) || laKind == (Super) || laKind == (Null) || laKind == (True) || laKind == (False)) { Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; } #region Literal if (laKind == Literal) { Step(); var startLoc = t.Location; // Concatenate multiple string literals here if (t.LiteralFormat == LiteralFormat.StringLiteral || t.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { var a = t.LiteralValue as string; while (la.LiteralFormat == LiteralFormat.StringLiteral || la.LiteralFormat == LiteralFormat.VerbatimStringLiteral) { Step(); a += t.LiteralValue as string; } return new IdentifierExpression(a, t.LiteralFormat) { Location = startLoc, EndLocation = t.EndLocation }; } //else if (t.LiteralFormat == LiteralFormat.CharLiteral)return new IdentifierExpression(t.LiteralValue) { LiteralFormat=t.LiteralFormat,Location = startLoc, EndLocation = t.EndLocation }; return new IdentifierExpression(t.LiteralValue, t.LiteralFormat) { Location = startLoc, EndLocation = t.EndLocation }; } #endregion #region ArrayLiteral | AssocArrayLiteral if (laKind == (OpenSquareBracket)) { Step(); var startLoc = t.Location; // Empty array literal if (laKind == CloseSquareBracket) { Step(); return new ArrayLiteralExpression() {Location=startLoc, EndLocation = t.EndLocation }; } var firstExpression = AssignExpression(); // Associtative array if (laKind == Colon) { Step(); var ae = new AssocArrayExpression() { Location=startLoc}; LastParsedObject = ae; var firstValueExpression = AssignExpression(); ae.KeyValuePairs.Add(firstExpression, firstValueExpression); while (laKind == Comma) { Step(); var keyExpr = AssignExpression(); Expect(Colon); var valueExpr = AssignExpression(); ae.KeyValuePairs.Add(keyExpr, valueExpr); } Expect(CloseSquareBracket); ae.EndLocation = t.EndLocation; return ae; } else // Normal array literal { var ae = new ArrayLiteralExpression() { Location=startLoc}; LastParsedObject = ae; var expressions = new List<IExpression>(); expressions.Add(firstExpression); while (laKind == Comma) { Step(); if (laKind == CloseSquareBracket) // And again, empty expressions are allowed break; expressions.Add(AssignExpression()); } ae.Expressions = expressions; Expect(CloseSquareBracket); ae.EndLocation = t.EndLocation; return ae; } } #endregion #region FunctionLiteral if (laKind == Delegate || laKind == Function || laKind == OpenCurlyBrace || (laKind == OpenParenthesis && IsFunctionLiteral())) { var fl = new FunctionLiteral() { Location=la.Location}; LastParsedObject = fl; fl.AnonymousMethod.StartLocation = la.Location; if (laKind == Delegate || laKind == Function) { Step(); fl.LiteralToken = t.Kind; } // file.d:1248 /* listdir (".", delegate bool (DirEntry * de) { auto s = std.string.format("%s : c %s, w %s, a %s", de.name, toUTCString (de.creationTime), toUTCString (de.lastWriteTime), toUTCString (de.lastAccessTime)); return true; } ); */ if (laKind != OpenCurlyBrace) // foo( 1, {bar();} ); -> is a legal delegate { if (!MemberFunctionAttribute[laKind] && Lexer.CurrentPeekToken.Kind == OpenParenthesis) fl.AnonymousMethod.Type = BasicType(); else if (laKind != OpenParenthesis && laKind != OpenCurlyBrace) fl.AnonymousMethod.Type = Type(); if (laKind == OpenParenthesis) fl.AnonymousMethod.Parameters = Parameters(fl.AnonymousMethod); } FunctionBody(fl.AnonymousMethod); fl.EndLocation = t.EndLocation; if (Scope != null) Scope.Add(fl.AnonymousMethod); return fl; } #endregion #region AssertExpression if (laKind == (Assert)) { Step(); var startLoc = t.Location; Expect(OpenParenthesis); var ce = new AssertExpression() { Location=startLoc}; LastParsedObject = ce; var exprs = new List<IExpression>(); exprs.Add(AssignExpression()); if (laKind == (Comma)) { Step(); exprs.Add(AssignExpression()); } ce.AssignExpressions = exprs.ToArray(); Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; } #endregion #region MixinExpression | ImportExpression if (laKind == Mixin) { Step(); var e = new MixinExpression() { Location=t.Location}; LastParsedObject = e; Expect(OpenParenthesis); e.AssignExpression = AssignExpression(); Expect(CloseParenthesis); e.EndLocation = t.EndLocation; return e; } if (laKind == Import) { Step(); var e = new ImportExpression() { Location=t.Location}; LastParsedObject = e; Expect(OpenParenthesis); e.AssignExpression = AssignExpression(); Expect(CloseParenthesis); e.EndLocation = t.EndLocation; return e; } #endregion if (laKind == (Typeof)) { var startLoc = la.Location; return new TypeDeclarationExpression(TypeOf()) {Location=startLoc,EndLocation=t.EndLocation}; } // TypeidExpression if (laKind == (Typeid)) { Step(); var ce = new TypeidExpression() { Location=t.Location}; LastParsedObject = ce; Expect(OpenParenthesis); AllowWeakTypeParsing = true; ce.Type = Type(); AllowWeakTypeParsing = false; if (ce.Type==null) ce.Expression = AssignExpression(); Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; } #region IsExpression if (laKind == Is) { Step(); var ce = new IsExpression() { Location=t.Location}; LastParsedObject = ce; Expect(OpenParenthesis); var LookAheadBackup = la; AllowWeakTypeParsing = true; ce.TestedType = Type(); AllowWeakTypeParsing = false; if (ce.TestedType!=null && laKind == Identifier && (Lexer.CurrentPeekToken.Kind == CloseParenthesis || Lexer.CurrentPeekToken.Kind == Equal || Lexer.CurrentPeekToken.Kind == Colon)) { Step(); ce.TypeAliasIdentifier = strVal; } else // D Language specs mistake: In an IsExpression there also can be expressions! if(ce.TestedType==null || !(laKind==CloseParenthesis || laKind==Equal||laKind==Colon)) { // Reset lookahead token to prior position la = LookAheadBackup; // Reset wrongly parsed type declaration ce.TestedType = null; ce.TestedExpression = ConditionalExpression(); } if(ce.TestedExpression==null && ce.TestedType==null) SynErr(laKind,"In an IsExpression, either a type or an expression is required!"); if (laKind == CloseParenthesis) { Step(); ce.EndLocation = t.EndLocation; return ce; } if (laKind == Colon || laKind == Equal) { Step(); ce.EqualityTest = t.Kind == Equal; } else if (laKind == CloseParenthesis) { Step(); ce.EndLocation = t.EndLocation; return ce; } /* TypeSpecialization: Type struct union class interface enum function delegate super const immutable inout shared return */ if (ClassLike[laKind] || laKind==Typedef || // typedef is possible although it's not yet documented in the syntax docs laKind==Enum || laKind==Delegate || laKind==Function || laKind==Super || laKind==Return) { Step(); ce.TypeSpecializationToken = t.Kind; } else ce.TypeSpecialization = Type(); if (laKind == Comma) { Step(); ce.TemplateParameterList = TemplateParameterList(false); } Expect(CloseParenthesis); ce.EndLocation = t.EndLocation; return ce; } #endregion // ( Expression ) if (laKind == OpenParenthesis) { Step(); var ret = new SurroundingParenthesesExpression() {Location=t.Location }; LastParsedObject = ret; ret.Expression = Expression(); Expect(CloseParenthesis); ret.EndLocation = t.EndLocation; return ret; } // TraitsExpression if (laKind == (__traits)) return TraitsExpression(); #region BasicType . Identifier if (laKind == (Const) || laKind == (Immutable) || laKind == (Shared) || laKind == (InOut) || BasicTypes[laKind]) { Step(); var startLoc = t.Location; IExpression left = null; if (!BasicTypes[t.Kind]) { int tk = t.Kind; // Put an artificial parenthesis around the following type declaration if (laKind != OpenParenthesis) { var mttd = new MemberFunctionAttributeDecl(tk); LastParsedObject = mttd; mttd.InnerType = Type(); left = new TypeDeclarationExpression(mttd) { Location = startLoc, EndLocation = t.EndLocation }; } else { Expect(OpenParenthesis); var mttd = new MemberFunctionAttributeDecl(tk); LastParsedObject = mttd; mttd.InnerType = Type(); Expect(CloseParenthesis); left = new TypeDeclarationExpression(mttd) { Location = startLoc, EndLocation = t.EndLocation }; } } else left = new TokenExpression(t.Kind) {Location=startLoc,EndLocation=t.EndLocation }; if (laKind == (Dot) && Peek(1).Kind==Identifier) { Step(); Step(); var meaex = new PostfixExpression_Access() { PostfixForeExpression=left, TemplateOrIdentifier=new IdentifierDeclaration(t.Value),EndLocation=t.EndLocation }; return meaex; } return left; } #endregion // TODO? Expressions can of course be empty... //return null; SynErr(Identifier); Step(); return new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; }
public override void Visit(TokenExpression e) { if (e.Token == DTokens.Incomplete) { halt = true; const MemberFilter BaseAsmFlags = MemberFilter.Classes | MemberFilter.StructsAndUnions | MemberFilter.Enums | MemberFilter.Methods | MemberFilter.TypeParameters | MemberFilter.Types | MemberFilter.Variables; if (scopedStatement is AsmStatement || scopedStatement is AsmStatement.InstructionStatement) prv = new CtrlSpaceCompletionProvider(cdgen, scopedBlock, scopedStatement, BaseAsmFlags | MemberFilter.x86Registers | MemberFilter.x64Registers | MemberFilter.Labels); else if (scopedStatement is AsmStatement.RawDataStatement) prv = new CtrlSpaceCompletionProvider(cdgen, scopedBlock, scopedStatement, BaseAsmFlags | MemberFilter.Labels); else if (handlesInitializer) prv = new CtrlSpaceCompletionProvider (cdgen, scopedBlock, scopedStatement); } }
public ISymbolValue Visit(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: if (resolveConstOnly) { EvalError(new NoConstException(x)); return(null); } var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) { classDef = classDef.Parent as IBlockNode; } /* * TODO: Return an object reference to the 'this' object. */ return(null); case DTokens.Super: // References super type of currently scoped class declaration if (resolveConstOnly) { EvalError(new NoConstException(x)); return(null); } /*classDef = ctxt.ScopedBlock; * * while (!(classDef is DClassLike) && classDef != null) * classDef = classDef.Parent as IBlockNode; * * if (classDef != null) * { * var tr = DResolver.ResolveBaseClasses(new ClassType(classDef as DClassLike, null, null), ctxt, true); * * if (tr.Base != null) * { * // Important: Overwrite type decl base with 'super' token * tr.Base.DeclarationOrExpressionBase = x; * * return tr.Base; * } * }*/ /* * TODO: Return an object reference to 'this', wheras the type is the superior type. */ return(null); case DTokens.Null: if (resolveConstOnly) { EvalError(new NoConstException(x)); return(null); } //TODO return(null); case DTokens.Dollar: // It's only allowed if the evaluation stack contains an array value if (ValueProvider.CurrentArrayLength != -1) { return(new PrimitiveValue(DTokens.Int, ValueProvider.CurrentArrayLength, x)); } else { EvalError(x, "Dollar not allowed here!"); return(null); } case DTokens.True: return(new PrimitiveValue(DTokens.Bool, 1, x)); case DTokens.False: return(new PrimitiveValue(DTokens.Bool, 0, x)); case DTokens.__FILE__: return(new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).FileName)); case DTokens.__LINE__: return(new PrimitiveValue(DTokens.Int, x.Location.Line, x)); case DTokens.__MODULE__: return(new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).ModuleName)); case DTokens.__FUNCTION__: //TODO return(null); case DTokens.__PRETTY_FUNCTION__: var dm = ctxt.ScopedStatement.ParentNode as DMethod; return(new ArrayValue(GetStringType(), dm == null ? "<not inside function>" : dm.ToString(false, true))); default: return(null); } }
ISemantic E(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: if (eval && resolveConstOnly) throw new NoConstException(x); var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; if (classDef is DClassLike) return TypeDeclarationResolver.HandleNodeMatch(classDef, ctxt, null, x); /* * TODO: Return an object reference to the 'this' object. */ break; case DTokens.Super: // References super type of currently scoped class declaration if (eval && resolveConstOnly) throw new NoConstException(x); classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; if (classDef != null) { var tr = DResolver.ResolveBaseClasses(new ClassType(classDef as DClassLike, null, null), ctxt, true); if (tr.Base != null) { // Important: Overwrite type decl base with 'super' token tr.Base.DeclarationOrExpressionBase = x; return tr.Base; } } /* * TODO: Return an object reference to 'this', wheras the type is the superior type. */ break; case DTokens.Null: if (eval && resolveConstOnly) throw new NoConstException(x); if (eval) { //TODO } return null; case DTokens.Dollar: // It's only allowed if the evaluation stack contains an array value if (ValueProvider.CurrentArrayLength != -1) return new PrimitiveValue(DTokens.Int, ValueProvider.CurrentArrayLength, x); else throw new EvaluationException(x, "Dollar not allowed here!"); case DTokens.True: return new PrimitiveValue(DTokens.Bool, 1, x); case DTokens.False: return new PrimitiveValue(DTokens.Bool, 0, x); case DTokens.__FILE__: return new ArrayValue(GetStringType(), x, (ctxt.ScopedBlock.NodeRoot as IAbstractSyntaxTree).FileName); case DTokens.__LINE__: return new PrimitiveValue(DTokens.Int, x.Location.Line, x); } return null; }
public string DeleteTest([FromBody] TokenExpression tokenExp) { return(JsonSerializer.Serialize(tokenExp)); }
ISemantic E(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: if (eval && resolveConstOnly) { throw new NoConstException(x); } var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) { classDef = classDef.Parent as IBlockNode; } if (classDef is DClassLike) { return(TypeDeclarationResolver.HandleNodeMatch(classDef, ctxt, null, x)); } /* * TODO: Return an object reference to the 'this' object. */ break; case DTokens.Super: // References super type of currently scoped class declaration if (eval && resolveConstOnly) { throw new NoConstException(x); } classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) { classDef = classDef.Parent as IBlockNode; } if (classDef != null) { var tr = DResolver.ResolveBaseClasses(new ClassType(classDef as DClassLike, null, null), ctxt, true); if (tr.Base != null) { // Important: Overwrite type decl base with 'super' token tr.Base.DeclarationOrExpressionBase = x; return(tr.Base); } } /* * TODO: Return an object reference to 'this', wheras the type is the superior type. */ break; case DTokens.Null: if (eval && resolveConstOnly) { throw new NoConstException(x); } if (eval) { //TODO } return(null); case DTokens.Dollar: // It's only allowed if the evaluation stack contains an array value if (ValueProvider.CurrentArrayLength != -1) { return(new PrimitiveValue(DTokens.Int, ValueProvider.CurrentArrayLength, x)); } else { throw new EvaluationException(x, "Dollar not allowed here!"); } case DTokens.True: return(new PrimitiveValue(DTokens.Bool, 1, x)); case DTokens.False: return(new PrimitiveValue(DTokens.Bool, 0, x)); case DTokens.__FILE__: return(new ArrayValue(GetStringType(), x, (ctxt.ScopedBlock.NodeRoot as IAbstractSyntaxTree).FileName)); case DTokens.__LINE__: return(new PrimitiveValue(DTokens.Int, x.Location.Line, x)); } return(null); }
public ExpressionValue Evaluate(Expression exp, Environment env) { if (exp == null) { return(new ExpressionValue(ExpressionValueType.BOOLEAN, false)); } switch (exp.kind) { case (ExpressionType.FUNCTION): { FunctionExpression ex = exp as FunctionExpression; return(this.vm.ExecuteFunction(ex)); } case (ExpressionType.FUNCT_DECL): { FunctionExpression decl = exp as FunctionExpression; return(new ExpressionValue(ExpressionValueType.FUNCTION, decl.function)); } case (ExpressionType.OBJECT): { return(new ExpressionValue(ExpressionValueType.OBJECT)); } case (ExpressionType.GET_OBJ): { AccessKeyExpression get = exp as AccessKeyExpression; List <string> accessor = get.AccessObj; ExpressionValue v = env.Get(accessor [0]) as ExpressionValue; for (int i = 1; i < accessor.Count; i++) { v = v.GetProperty(accessor [i]); } return(v); } case (ExpressionType.IDENTIFIER): { TokenExpression tok = exp as TokenExpression; string id = tok.token.Value; return(env.Get(id) as ExpressionValue); } case (ExpressionType.BOOL): { return(exp.value); } case (ExpressionType.STRING): { return(exp.value); } case (ExpressionType.INTEGER): { return(exp.value); } case (ExpressionType.ADD): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); if (v1.IsString) { return(new ExpressionValue(ExpressionValueType.STRING, v2.String + v1.String)); } else { return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number + v2.Number)); } } case (ExpressionType.SUBS): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number - v2.Number)); } case (ExpressionType.MUL): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number * v2.Number)); } case (ExpressionType.DIV): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number / v2.Number)); } case (ExpressionType.AND): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Bool && v2.Bool)); } case (ExpressionType.OR): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Bool || v2.Bool)); } case (ExpressionType.EQUAL): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.BOOLEAN, (v1.Bool == v2.Bool) && (v1.Number == v2.Number) && (v1.String == v2.String))); } case (ExpressionType.DISEQUAL): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.BOOLEAN, (v1.Bool != v2.Bool) && (v1.Number != v2.Number) && (v1.String != v2.String))); } case (ExpressionType.LESS): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Number < v2.Number)); } case (ExpressionType.GREATER): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number > v2.Number)); } case (ExpressionType.LESS_OR_EQUAL): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number <= v2.Number)); } case (ExpressionType.GREATER_OR_EQUAL): { OperationExpression op = exp as OperationExpression; ExpressionValue v1 = this.Evaluate(op.lhs, env); ExpressionValue v2 = this.Evaluate(op.rhs, env); return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number >= v2.Number)); } default: return(null); } }
public TemplateInstanceExpression TemplateInstance() { if (!Expect(Identifier)) return null; var td = new TemplateInstanceExpression() { TemplateIdentifier = new IdentifierDeclaration(t.Value) { Location=t.Location, EndLocation=t.EndLocation }, Location = t.Location }; LastParsedObject = td; var args = new List<IExpression>(); if (!Expect(Not)) return td; if (laKind == (OpenParenthesis)) { Step(); if (laKind != CloseParenthesis) { bool init = true; while (laKind == Comma || init) { if (!init) Step(); init = false; if (IsEOF) { args.Add(new TokenExpression(DTokens.INVALID) { Location= la.Location, EndLocation=la.EndLocation }); break; } var la_Backup = la; bool wp = AllowWeakTypeParsing; AllowWeakTypeParsing = true; var typeArg = Type(); AllowWeakTypeParsing = wp; if (typeArg != null && (laKind == CloseParenthesis || laKind==Comma)) args.Add(new TypeDeclarationExpression(typeArg)); else { la = la_Backup; Peek(1); args.Add(AssignExpression()); } } } Expect(CloseParenthesis); } else { Step(); /* * TemplateSingleArgument: * Identifier * BasicTypeX * CharacterLiteral * StringLiteral * IntegerLiteral * FloatLiteral * true * false * null * __FILE__ * __LINE__ */ IExpression arg= null; if (t.Kind == Literal) arg = new IdentifierExpression(t.LiteralValue, LiteralFormat.Scalar) { Location = t.Location, EndLocation = t.EndLocation }; else if (t.Kind == Identifier) arg = new IdentifierExpression(t.Value) { Location = t.Location, EndLocation = t.EndLocation }; else if (BasicTypes[t.Kind]) arg = new TypeDeclarationExpression(new DTokenDeclaration(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }); else if ( t.Kind == True || t.Kind == False || t.Kind == Null || t.Kind == __FILE__ || t.Kind == __LINE__) arg = new TokenExpression(t.Kind) { Location = t.Location, EndLocation = t.EndLocation }; else if (IsEOF) { ExpectingIdentifier = false; return td; } args.Add(arg); } td.Arguments = args.ToArray(); td.EndLocation = t.EndLocation; return td; }
public ISymbolValue Visit(TokenExpression x) { switch (x.Token) { // References current class scope case DTokens.This: if (resolveConstOnly) { EvalError(new NoConstException(x)); return null; } var classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; /* * TODO: Return an object reference to the 'this' object. */ return null; case DTokens.Super: // References super type of currently scoped class declaration if (resolveConstOnly) { EvalError(new NoConstException(x)); return null; } /*classDef = ctxt.ScopedBlock; while (!(classDef is DClassLike) && classDef != null) classDef = classDef.Parent as IBlockNode; if (classDef != null) { var tr = DResolver.ResolveBaseClasses(new ClassType(classDef as DClassLike, null, null), ctxt, true); if (tr.Base != null) { // Important: Overwrite type decl base with 'super' token tr.Base.DeclarationOrExpressionBase = x; return tr.Base; } }*/ /* * TODO: Return an object reference to 'this', wheras the type is the superior type. */ return null; case DTokens.Null: if (resolveConstOnly) { EvalError(new NoConstException(x)); return null; } //TODO return null; case DTokens.Dollar: // It's only allowed if the evaluation stack contains an array value if (ValueProvider.CurrentArrayLength != -1) return new PrimitiveValue(ValueProvider.CurrentArrayLength); else { EvalError(x, "Dollar not allowed here!"); return null; } case DTokens.True: return new PrimitiveValue(true); case DTokens.False: return new PrimitiveValue(false); case DTokens.__FILE__: return new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).FileName); case DTokens.__LINE__: return new PrimitiveValue(x.Location.Line); case DTokens.__MODULE__: return new ArrayValue(GetStringType(), (ctxt.ScopedBlock.NodeRoot as DModule).ModuleName); case DTokens.__FUNCTION__: //TODO return null; case DTokens.__PRETTY_FUNCTION__: var dm = ctxt.ScopedBlock as DMethod; return new ArrayValue(GetStringType(), dm == null ? "<not inside function>" : dm.ToString(false, true)); default: return null; } }