示例#1
0
def parse_port(next, infile, entity):
    '''parse the port and return a 'port' object
    '''
    names = []

    while 1:
        cont = compareIdentifiers(next, Identifier(" "), infile)
        names.append(next.name)
        if not isinstance(cont, Token) or cont.name != ",":
            break
        else:
            next = nextToken(infile)
            
    valueOne = compareTokens(cont, Token(":"), infile)
    (valueTwo, flow) = parse_flow(valueOne, infile)
    (valueThree, type, length) = parse_datatype(valueTwo, infile)

    # store derived port information into Entity
    for i in names:
        if flow == "in":
            entity.IN.append(Port.Port(i, Port.IN, type, length))
        elif flow == "out":
            entity.OUT.append(Port.Port(i, Port.OUT, type, length))
        else:
            entity.INOUT.append(Port.Port(i, Port.INOUT, type, length))

    # recurse if there are more ports defined
    if isinstance (valueThree, Token) and valueThree.name == ";":
        return parse_port(nextToken(infile), infile, entity)
    else:
        return (valueThree, entity)
示例#2
0
def parse_datatype(current, infile):
    '''parse the datatype and return Trit object that identifies the datatype
    '''
    if isinstance (current, Keyword):
        if current.name == "trit":
            return (nextToken(infile), "trit", 0)
        next =  compareKeywords(current, Keyword("trit_vector"), infile)

        valueOne = compareTokens(next, Token("("), infile)

        if not isinstance(valueOne, Literal):
            printError(valueOne, Literal("integer"))
        elif valueOne.value <= 0:
            printError(valueOne, Literal("greater than zero"))
        valueTwo = nextToken(infile)

        length = valueOne.value + 1

        valueThree = compareTokens(valueTwo, Keyword("downto"), infile)
        if not isinstance(valueThree, Literal):
            printError(valueThree, Literal("integer"))
        elif valueThree.value != 0:
            printError(valueThree, Literal(0))
        valueFour = nextToken(infile)
        
        valueFive = compareTokens(valueFour, Token(")"), infile)
        
        # construct datatype object for trit_vector and return it
        # along with the next token
        return (valueFive, "trit_vector", length)

    printError(current, Keyword("trit|trit_vector"))
示例#3
0
def parse_flow(current, infile):
    '''identify the direction of the flow of the port
    '''
    if isinstance(current, Keyword):
        if current.name == "in":
            return (nextToken(infile), "in")
        elif current.name == "out":
            return (nextToken(infile), "out")
        elif current.name == "inout":
            return (nextToken(infile), "inout")

    printError(current, Keyword("in|out|inout"))
示例#4
0
def compareKeywords(current, expected, infile):
    '''compare current keyword with expected keyword. If they are equal then return
       the next token. If they are not then an raise and exeption and exit the program.
    '''
    if not isinstance(current, Keyword) or  current.name != expected.name:
        raise "Expected '%s', found '%s'" % (expected, current)
    else:
        print "parsed: %s" % current #for debugging
        return nextToken(infile)
示例#5
0
def compareIdentifiers(current, expected, infile):
    '''compare current identifier with expected identifier. If they are equal then return
       the next token. If they are not then an raise and exeption and exit the program.
    '''
    if not isinstance(current, Identifier):
        raise "Expected '%s', found '%s'" % (expected, current)
    else:
        print "parsed: %s" % current #for debugging
        return nextToken(infile)
	def readForm(self, parent, text, openedParenthesis, maximum=-1):
		(token, text) = tokenizer.nextToken(text)
		while token.tokenId != EOF:
			if token.tokenId == SYNTAX_ERROR:
				raise BadInputException("Syntax error")
			
			if token.tokenId == QUOTE or token.tokenId == BACKQUOTE or token.tokenId == COMMA or token.tokenId == HASH or token.tokenId == COMMA_AT:
				newForm = List()
				newForm.children.append(Symbol(token.value))
				text = self.readForm(newForm, text, False, 1)[1]
				parent.children.append(newForm)
			else:
				newForm = LispForm.createLispForm(token.tokenId, token.value)
				
				if token.tokenId == OPENING_PARENTHESIS:
					text = self.readForm(newForm, text, True)[1]
					parent.children.append(newForm)
				elif token.tokenId == CLOSING_PARENTHESIS:
					if not(openedParenthesis):
						raise BadInputException("Unexpected ')'")
					return (newForm, text)
				elif token.tokenId == INT:
					newForm.value = int(newForm.value)
					parent.children.append(newForm)
				else:
					parent.children.append(newForm)
			
			maximum -= 1
			if maximum == 0:
				return (newForm, text)
			(token, text) = tokenizer.nextToken(text)	
		
		if openedParenthesis:
			raise BadInputException("Unexpected end of file")

		return (newForm, text)
示例#7
0
def Parser(filename):
    '''Parse a file into components.
       Arguments (1): filename of the file to parse.
    '''
    infile = file(filename, "r")
    parse_program(nextToken(infile), infile)
	def read(self, text):
		form = List()
		text = self.readForm(form, text, False)[1]
		if tokenizer.nextToken(text)[0].tokenId != EOF:
			raise BadInputException("Syntax error")
		return form.children