public string Visit(PyAst.ConstantExpression node) { string formatString(string rawString) { foreach (var charToEscape in charsToEscape) { if (rawString.Contains(charToEscape)) { rawString = rawString.Replace(charToEscape, "\\" + charToEscape); } } return($"\"{ rawString }\""); } switch (node.Value) { case double val: if (Math.Truncate(val) == val) { // If we don't do this, the double value 1.0 will be output as integer 1 return(val.ToString("0.0")); } return(node.Value.ToString()); case char val: return(formatString(val.ToString())); case string val: return(formatString(val)); case int val: return(node.Value.ToString()); case bool val: return(val ? "True" : "False"); case null: return("None"); default: throw new NotImplementedException($"Printing of constant expression {node.Value.GetType()} not implemented"); } }
// primary: atom | attributeref | subscription | slicing | call // atom: identifier | literal | enclosure // enclosure: // parenth_form | // list_display | // generator_expression | // dict_display | // string_conversion | // yield_atom private Expression ParsePrimary() { Token t = PeekToken(); Expression ret; switch (t.Kind) { case TokenKind.LeftParenthesis: // parenth_form, generator_expression, yield_atom NextToken(); return FinishTupleOrGenExp(); case TokenKind.LeftBracket: // list_display NextToken(); return FinishListValue(); case TokenKind.LeftBrace: // dict_display NextToken(); return FinishDictOrSetValue(); case TokenKind.BackQuote: // string_conversion NextToken(); return FinishStringConversion(); case TokenKind.Name: // identifier NextToken(); string name = (string)t.Value; if (_sink != null) { _sink.StartName(GetSourceSpan(), name); } ret = new NameExpression(FixName(name)); ret.SetLoc(_globalParent, GetStart(), GetEnd()); return ret; case TokenKind.Constant: // literal NextToken(); var start = GetStart(); object cv = t.Value; string cvs = cv as string; if (cvs != null) { cv = FinishStringPlus(cvs); } else { Bytes bytes = cv as Bytes; if (bytes != null) { cv = FinishBytesPlus(bytes); } } if (t is UnicodeStringToken) { ret = ConstantExpression.MakeUnicode((string)cv); } else { ret = new ConstantExpression(cv); } ret.SetLoc(_globalParent, start, GetEnd()); return ret; default: ReportSyntaxError(_lookahead.Token, _lookahead.Span, ErrorCodes.SyntaxError, _allowIncomplete || _tokenizer.EndContinues); // error node ret = new ErrorExpression(); ret.SetLoc(_globalParent, _lookahead.Span.Start, _lookahead.Span.End); return ret; } }
internal static expr Convert(ConstantExpression expr) { expr ast; if (expr.Value == null) return new Name("None", Load.Instance); #if CLR2 if (expr.Value is int || expr.Value is double || expr.Value is Int64 || expr.Value is BigInteger || expr.Value is Complex64) #else if (expr.Value is int || expr.Value is double || expr.Value is Int64 || expr.Value is BigInteger || expr.Value is Complex) #endif ast = new Num(expr.Value); else if (expr.Value is string) ast = new Str((string)expr.Value); else if (expr.Value is IronPython.Runtime.Bytes) ast = new Str(Converter.ConvertToString(expr.Value)); else if (expr.Value == PythonOps.Ellipsis) ast = Ellipsis.Instance; else throw new ArgumentTypeException("Unexpected constant type: " + expr.Value.GetType()); return ast; }
public override bool Walk(ConstantExpression node) { if (!FoundInitializeComponentMethod) { return false; } fieldExpression.SetPropertyValue(componentCreator, node.Value); return false; }
public override void PostWalk(ConstantExpression node) { }
public override bool Walk(ConstantExpression node) { Content(WriteValue(node.Value)); return false; }
/// <summary> /// Peek if the next token is a 'yield' and parse a yield expression. Else return null. /// /// Called w/ yield already eaten. /// </summary> /// <returns>A yield expression if present, else null. </returns> // yield_expression: "yield" [expression_list] private Expression ParseYieldExpression() { // Mark that this function is actually a generator. // If we're in a generator expression, then we don't have a function yet. // g=((yield i) for i in range(5)) // In that acse, the genexp will mark IsGenerator. FunctionDefinition current = CurrentFunction; if (current != null) { current.IsGenerator = true; } SourceLocation start = GetStart(); // Parse expression list after yield. This can be: // 1) empty, in which case it becomes 'yield None' // 2) a single expression // 3) multiple expression, in which case it's wrapped in a tuple. const bool allowEmptyExpr = true; Expression yieldResult = ParseTestListAsExpr(allowEmptyExpr); // Check empty expression and convert to 'none' TupleExpression t = yieldResult as TupleExpression; if (t != null) { if (t.Items.Length == 0) { yieldResult = new ConstantExpression(null); } } Expression yieldExpression = new YieldExpression(yieldResult); yieldExpression.SetLoc(start, GetEnd()); return yieldExpression; }
internal override AstExpression Revert() { AstExpression index = null; if (slice is Index) index = expr.Revert(((Index)slice).value); else if (slice is Slice) { Slice concreteSlice = (Slice)slice; AstExpression start = null; if (concreteSlice.lower != null) start = expr.Revert(concreteSlice.lower); AstExpression stop = null; if (concreteSlice.upper != null) stop = expr.Revert(concreteSlice.upper); AstExpression step = null; bool stepProvided = false; if (concreteSlice.step != null) { stepProvided = true; if (concreteSlice.step is Name && ((Name)concreteSlice.step).id == "None") { // pass } else { step = expr.Revert(concreteSlice.step); } } index = new SliceExpression(start, stop, step, stepProvided); } else if (slice is Ellipsis) { index = new ConstantExpression(PythonOps.Ellipsis); } else if (slice is ExtSlice) { index = new TupleExpression(true, ((ExtSlice)slice).Revert()); } else { Debug.Assert(false, "Unexpected type when converting Subscript: " + slice.GetType()); } return new IndexExpression(expr.Revert(value), index); }
// ConstantExpression public bool Walk(ConstantExpression node) { return Process(node); }
// ConstantExpression public override bool Walk(ConstantExpression node) { node.Parent = _currentScope; return(base.Walk(node)); }
object Deserialize(ConstantExpression expression) { return expression.Value; }
public virtual void PostWalk(ConstantExpression node) { }
// ConstantExpression public virtual bool Walk(ConstantExpression node) { return true; }
// atom: '(' [testlist_gexp] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+ private Expression ParsePrimary() { Token t = NextToken(); Expression ret; switch (t.Kind) { case TokenKind.LeftParenthesis: return FinishTupleOrGenExp(); case TokenKind.LeftBracket: return FinishListValue(); case TokenKind.LeftBrace: return FinishDictValue(); case TokenKind.BackQuote: return FinishBackquote(); case TokenKind.Name: CodeSpan span = GetSpan(); SymbolId name = (SymbolId)t.Value; context.Sink.StartName(span, name.GetString()); ret = new NameExpression(FixName(name)); ret.SetLoc(GetExternal(), GetStart(), GetEnd()); return ret; case TokenKind.Constant: Location start = GetStart(); object cv = t.Value; if (cv is String) { cv = FinishStringPlus((string)cv); } // todo handle STRING+ ret = new ConstantExpression(cv); ret.SetLoc(GetExternal(), start, GetEnd()); return ret; default: ReportSyntaxError(t, ErrorCodes.SyntaxError, allowingIncomplete); // error node ret = new ErrorExpression(); ret.SetLoc(GetExternal(), GetStart(), GetEnd()); return ret; } }
//subscriptlist: subscript (',' subscript)* [','] //subscript: '.' '.' '.' | expression | [expression] ':' [expression] [sliceop] //sliceop: ':' [expression] private Expression ParseSubscriptList() { const TokenKind terminator = TokenKind.RightBracket; var start0 = GetStart(); bool trailingComma = false; List<Expression> l = new List<Expression>(); while (true) { Expression e; if (MaybeEat(TokenKind.Dot)) { var start = GetStart(); Eat(TokenKind.Dot); Eat(TokenKind.Dot); e = new ConstantExpression(Ellipsis.Value); e.SetLoc(_globalParent, start, GetEnd()); } else if (MaybeEat(TokenKind.Colon)) { e = FinishSlice(null, GetStart()); } else { e = ParseExpression(); if (MaybeEat(TokenKind.Colon)) { e = FinishSlice(e, e.StartIndex); } } l.Add(e); if (!MaybeEat(TokenKind.Comma)) { Eat(terminator); trailingComma = false; break; } trailingComma = true; if (MaybeEat(terminator)) { break; } } Expression ret = MakeTupleOrExpr(l, trailingComma, true); ret.SetLoc(_globalParent, start0, GetEnd()); return ret; }
public void PostWalk(ConstantExpression node) { PostProcess(node); }
/// <summary> /// Peek if the next token is a 'yield' and parse a yield expression. Else return null. /// /// Called w/ yield already eaten. /// </summary> /// <returns>A yield expression if present, else null. </returns> // yield_expression: "yield" [expression_list] private Expression ParseYieldExpression() { // Mark that this function is actually a generator. // If we're in a generator expression, then we don't have a function yet. // g=((yield i) for i in range(5)) // In that acse, the genexp will mark IsGenerator. FunctionDefinition current = CurrentFunction; if (current != null) { current.IsGenerator = true; } var start = GetStart(); // Parse expression list after yield. This can be: // 1) empty, in which case it becomes 'yield None' // 2) a single expression // 3) multiple expression, in which case it's wrapped in a tuple. Expression yieldResult; bool trailingComma; List<Expression> l = ParseExpressionList(out trailingComma); if (l.Count == 0) { // Check empty expression and convert to 'none' yieldResult = new ConstantExpression(null); } else if (l.Count != 1) { // make a tuple yieldResult = MakeTupleOrExpr(l, trailingComma); } else { // just take the single expression yieldResult = l[0]; } Expression yieldExpression = new YieldExpression(yieldResult); yieldExpression.SetLoc(_globalParent, start, GetEnd()); return yieldExpression; }
public override bool Walk(ConstantExpression node) { writer.WriteLine("ConstantExpression"); return base.Walk(node); }