Inheritance: Expression, IInstructionProvider
Esempio n. 1
0
        // dict_display: '{' [dictorsetmaker] '}'
        // dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        //                   (test (comp_for | (',' test)* [','])) )


        private Expression FinishDictOrSetValue() {
            var oStart = GetStart();
            var oEnd = GetEnd();

            List<SliceExpression> dictMembers = null;
            List<Expression> setMembers = null;
            bool prevAllow = _allowIncomplete;
            try {
                _allowIncomplete = true;
                while (true) {
                    if (MaybeEat(TokenKind.RightBrace)) { // empty dict literal
                        break;
                    }
                    bool first = false;
                    Expression e1 = ParseExpression();
                    if (MaybeEat(TokenKind.Colon)) { // dict literal
                        if (setMembers != null) {
                            ReportSyntaxError("invalid syntax");
                        } else if (dictMembers == null) {
                            dictMembers = new List<SliceExpression>();
                            first = true;
                        }
                        Expression e2 = ParseExpression();

                        if (PeekToken(Tokens.KeywordForToken)) {
                            if (!first) {
                                ReportSyntaxError("invalid syntax");
                            }
                            return FinishDictComp(e1, e2, oStart, oEnd);
                        }

                        SliceExpression se = new SliceExpression(e1, e2, null, false);
                        se.SetLoc(_globalParent, e1.StartIndex, e2.EndIndex);
                        dictMembers.Add(se);
                    } else { // set literal
                        if (dictMembers != null) {
                            ReportSyntaxError("invalid syntax");
                        } else if (setMembers == null) {
                            setMembers = new List<Expression>();
                            first = true;
                        }

                        if (PeekToken(Tokens.KeywordForToken)) {
                            if (!first) {
                                ReportSyntaxError("invalid syntax");
                            }
                            return FinishSetComp(e1, oStart, oEnd);
                        }

                        // error recovery
                        if (setMembers != null) {
                            setMembers.Add(e1);
                        }
                    }

                    if (!MaybeEat(TokenKind.Comma)) {
                        Eat(TokenKind.RightBrace);
                        break;
                    }
                }
            } finally {
                _allowIncomplete = prevAllow;
            }
            

            var cStart = GetStart();
            var cEnd = GetEnd();

            if (_sink != null) {
                _sink.MatchPair(
                    new SourceSpan(_tokenizer.IndexToLocation(oStart), _tokenizer.IndexToLocation(oEnd)), 
                    new SourceSpan(_tokenizer.IndexToLocation(cStart), _tokenizer.IndexToLocation(cEnd)), 
                    1
                );
            }

            if (dictMembers != null || setMembers == null) {
                SliceExpression[] exprs;
                if (dictMembers != null) {
                    exprs = dictMembers.ToArray();
                } else {
                    exprs = new SliceExpression[0];
                }
                DictionaryExpression ret = new DictionaryExpression(exprs);
                ret.SetLoc(_globalParent, oStart, cEnd);
                return ret;
            } else {
                SetExpression ret = new SetExpression(setMembers.ToArray());
                ret.SetLoc(_globalParent, oStart, cEnd);
                return ret;
            }
        }
Esempio n. 2
0
 internal Dict(DictionaryExpression expr)
     : this() {
     _keys = PythonOps.MakeEmptyList(expr.Items.Count);
     _values = PythonOps.MakeEmptyList(expr.Items.Count);
     foreach (SliceExpression item in expr.Items) {
         _keys.Add(Convert(item.SliceStart));
         _values.Add(Convert(item.SliceStop));
     }
 }
Esempio n. 3
0
        // dict_display: '{' [key_datum_list] '}'
        // key_datum_list: key_datum (',' key_datum)* [","]
        // key_datum: expression ':' expression
        private Expression FinishDictValue() {
            SourceLocation oStart = GetStart();
            SourceLocation oEnd = GetEnd();

            List<SliceExpression> l = new List<SliceExpression>();
            bool prevAllow = _allowIncomplete;
            try {
                _allowIncomplete = true;
                while (true) {
                    if (MaybeEat(TokenKind.RightBrace)) {
                        break;
                    }
                    Expression e1 = ParseExpression();
                    Eat(TokenKind.Colon);
                    Expression e2 = ParseExpression();
                    SliceExpression se = new SliceExpression(e1, e2, null, false);
                    se.SetLoc(e1.Start, e2.End);
                    l.Add(se);

                    if (!MaybeEat(TokenKind.Comma)) {
                        Eat(TokenKind.RightBrace);
                        break;
                    }
                }
            } finally {
                _allowIncomplete = prevAllow;
            }

            SourceLocation cStart = GetStart();
            SourceLocation cEnd = GetEnd();

            _sink.MatchPair(new SourceSpan(oStart, oEnd), new SourceSpan(cStart, cEnd), 1);

            SliceExpression[] exprs = l.ToArray();
            DictionaryExpression ret = new DictionaryExpression(exprs);
            ret.SetLoc(oStart, cEnd);
            return ret;
        }
Esempio n. 4
0
        public override void PostWalk(DictionaryExpression node)
        {
            EndIndent();

            string ind = Indent(indent + 1);
            List<string> items = new List<string>();
            for (int i = 0; i < node.Items.Count; i++)
            {
                items.Add(ind + Content());
            }
            items.Reverse();

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("{");

            sb.AppendLine(String.Join(",\n", items));

            sb.Append(Indent());
            sb.Append("}");

            Content(sb.ToString());

            CommonPostWalk(node, true);
        }
Esempio n. 5
0
 public override bool Walk(DictionaryExpression node)
 {
     node.Parent = _currentScope;
     return base.Walk(node);
 }
 // DictionaryExpression
 public bool Walk(DictionaryExpression node)
 {
     return Process(node);
 }
Esempio n. 7
0
        public override bool Walk(DictionaryExpression node)
        {
            CommonWalk(node);

            BeginIndent();

            return true;
        }
Esempio n. 8
0
 // DictionaryExpression
 public override bool Walk(DictionaryExpression node)
 {
     node.Parent = _currentScope;
     return(base.Walk(node));
 }
 public void PostWalk(DictionaryExpression node)
 {
     PostProcess(node);
 }
Esempio n. 10
0
 public static string Format(DictionaryExpression node)
 {
     return("{" + node.Items.Format() + "}");
 }
 public virtual void PostWalk(DictionaryExpression node)
 {
 }
 // DictionaryExpression
 public virtual bool Walk(DictionaryExpression node)
 {
     return true;
 }
Esempio n. 13
0
 public string Visit(PyAst.DictionaryExpression node) => throw CreateNotImplementedEx();
Esempio n. 14
0
		public override bool Walk(DictionaryExpression node)
		{
			writer.WriteLine("Dict");
			return base.Walk(node);
		}