def statement(self, allow_music_shortcut=False): """ statement: assignment | block | include | import allow_music_shortcut can be set when we are parsing assignments in a question block. """ if self._lexer.peek_type(1) == '=': return self.assignment() elif self._lexer.peek_type(1) == '{': return self.block() elif self._lexer.peek_type(1) == 'NAME' \ and self._lexer.peek_type(2) == '{': return self.named_block() elif self._lexer.peek_type() == 'NAME' \ and self._lexer.peek_string() == 'include' \ and self._lexer.peek_type(1) == '(': return self.include() elif (self._lexer.peek_type() == 'NAME' and self._lexer.peek_string() == 'import'): return self.do_import() elif (self._lexer.peek_type() == 'NAME' and self._lexer.peek_string() == 'rimport'): return self.do_rimport() elif allow_music_shortcut: fakt = self.expressionlist() if self._lexer.peek_type() == '}': return pt.Assignment(pt.Identifier("music"), fakt[0]) else: raise DataparserSyntaxError( self, self._lexer.pos, "The (obsolete) music shortcut construct must be the last statement in a question block." ) self._lexer.scan_any() raise DataparserSyntaxError(self, self._lexer.pos, "Parse error")
def assignment(self): """NAME "=" expression ("," expression)* """ npos = self._lexer.pos name = self._lexer.scan_any() # ('NAME') if name in self.reserved_words: # do "question = 1" to raise this exception. raise AssignmentToReservedWordException(self, npos, name) self._lexer.scan_any() # ('=') fpos = self._lexer.pos expressionlist = self.expressionlist() m = self.m_translation_re.match(name) if m: if len(expressionlist) != 1: raise CanOnlyTranslateStringsException(self, fpos, name) if not isinstance(expressionlist[0].m_value, istr): raise CanOnlyTranslateStringsException(self, fpos, name) if len(expressionlist) == 1: return pt.Assignment(pt.Identifier(name), expressionlist[0]) else: return pt.Assignment(pt.Identifier(name), expressionlist)
def _import_worker(self, fn1, fn2): self._lexer.scan_any() # scan the 'import' or 'rimport' keyword mod_filename = self._lexer.scan_any() if (self._lexer.peek_type() == 'NAME' and self._lexer.peek_string() == 'as'): self._lexer.scan_any() mod_name = self._lexer.scan('NAME') else: mod_name = mod_filename p = Dataparser() fn1 = os.path.join(fn1, mod_filename) fn2 = os.path.join(fn2, mod_filename) if os.path.exists(fn1) or not os.path.exists(fn2): p.parse_file(fn1) else: p.parse_file(fn2) return pt.Assignment(pt.Identifier(mod_name), p.tree)