Ejemplo n.º 1
0
func scan(c *scanner.Scanner) token {
	c.EatWhitespaceAndComments()
	if c.Peek() == '/' {
		c.ReadChar()
		return token{"Divide", ""}
	} else if c.EndOfFile() {
		log.Println("Found EOF")
		return token{"EOF", ""}
	} else if c.Peek() == '-' {
		c.ReadChar()
		chr := c.ReadChar()
		if chr != '>' {
			c.Fail("Found -, and expected a \"to\" statement(->) but found ", chr)
		}
		return token{"To", ""}
	} else if utils.IsAlphabetic(c.Peek()) {
		word := c.ReadWord()
		if word == "States" {
			return token{"States", ""}
		} else if word == "Edges" {
			return token{"Edges", ""}
		}
		return token{"Word", word}

	} else if utils.IsNumeric(c.Peek()) {
		integer := strconv.Itoa(c.ReadNumber())
		return token{"Integer", integer}
	} else {
		c.Fail("Unexpected keyword ", c.Peek())
	}
	return token{}
}
Ejemplo n.º 2
0
func (c *Scanner) ReadWord() string {
	if !utils.IsAlphabetic(c.Peek()) {
		c.Fail("Could not read word")
	}
	word := string(c.ReadChar())
	for !c.EndOfFile() {
		if utils.IsAlphabetic(c.Peek()) || utils.IsNumeric(c.Peek()) {
			word += string(c.ReadChar())
		} else if utils.IsWhitespace(c.Peek()) || c.Peek() == '/' {
			break
		} else {
			c.Fail("Reading a word failed, found '", word, "' but then unexpected ", string(c.Peek()))
		}
	}
	return word
}
Ejemplo n.º 3
0
func (c *Scanner) ReadNumber() int {
	number := ""
	for !c.EndOfFile() {
		if utils.IsNumeric(c.Peek()) {
			number += string(c.ReadChar())
		} else if utils.IsWhitespace(c.Peek()) || c.Peek() == '/' {
			break
		} else {
			c.Fail("Expected whitespace after the number "+number+", but got ", string(c.Peek()))
		}
	}

	if len(number) == 0 {
		c.Fail("Did not read any number")
	}

	numbasint, err := strconv.Atoi(number)
	if err != nil {
		c.Fail("Could not convert to number. Something really bad happened, and should never happen. Please contact the developers at https://github.com/jbossen/P7-code")
	}

	return numbasint
}