Ejemplo n.º 1
0
		// YOU LEFT OFF HERE
		// You were about to go through and document exactly what each indention variable meant more clearly.
		private IList<Executable> ParseBlock(TokenStream tokens, int ownerIndention, bool start)
		{
			int blockIndention = tokens.PeekIndention();
			if (start)
			{
				if (blockIndention != 0)
				{
					throw new ParserException(tokens.Peek(), "Unexpected indention");
				}
			}
			else
			{
				if (blockIndention == -1)
				{
					// This indicates the code is on the same line. Parse one line.
					// def foo(): return 42
					Executable exec = this.Parse(tokens, false, -1);
					return new List<Executable>() { exec };
				}

				if (blockIndention <= ownerIndention)
				{
					// No indention was found. But it is required.
					throw new ParserException(tokens.Peek(), "Expected: indention");
				}
			}

			int requiredIndention = blockIndention;
			
			List<Executable> code = new List<Executable>();
			while (tokens.HasMore)
			{
				int currentIndention = tokens.PeekIndention();

				// any new indention should be handled by a recursive call
				if (currentIndention > requiredIndention) throw new ParserException(tokens.Peek(), "Unexpected indention");

				// if it's indented less than the required but more than the previous, then that's not right
				// e.g.
				// def foo()
				//     x = 1
				//   return x # this is wrong
				if (currentIndention < requiredIndention && currentIndention > ownerIndention) throw new ParserException(tokens.Peek(), "Unexpected indention");

				// def foo()
				//     x = 42
				// y = 3.14 # this is no longer part of foo()
				// start is excluded because when start is true, the owner indention and current indention are the same.
				if (!start && currentIndention <= ownerIndention) return code;

				tokens.SkipWhitespace();
				if (tokens.HasMore)
				{
					code.Add(this.Parse(tokens, true, currentIndention));
				}
				else
				{
					return code;
				}
			}
			return code;
		}