Exemplo n.º 1
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
}
Exemplo n.º 2
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{}
}