def parse_document( txt: str, version: Optional[str] = None, uri: str = "", imported: bool = False ) -> D.Document: if version is None: # for now assume the version is 1.0 if the first line is "version <number>" # otherwise draft-2 version = "draft-2" for line in txt.split("\n"): line = line.strip() if line and line[0] != "#": if line.startswith("version ") and line[8].isdigit(): version = "1.0" break if not txt.strip(): return D.Document( SourcePosition(filename=uri, line=0, column=0, end_line=0, end_column=0), [], [], None, imported, ) try: return _DocTransformer(uri, imported).transform(parse(txt, "document", version)) except lark.exceptions.UnexpectedCharacters as exn: raise Err.ParserError(uri if uri != "" else "(in buffer)") from exn except lark.exceptions.UnexpectedToken as exn: raise Err.ParserError(uri if uri != "" else "(in buffer)") from exn
def document(self, items, meta): imports = [] structs = {} tasks = [] workflow = None for item in items: if isinstance(item, D.Task): tasks.append(item) elif isinstance(item, D.Workflow): if workflow is not None: raise Err.MultipleDefinitions( sp(self.filename, meta), "Document has multiple workflows") workflow = item elif isinstance(item, D.StructTypeDef): if item.name in structs: raise Err.MultipleDefinitions( sp(self.filename, meta), "multiple structs named " + item.name) structs[item.name] = item elif isinstance(item, lark.Tree) and item.data == "version": pass elif isinstance(item, D.DocImport): imports.append(item) else: assert False return D.Document(sp(self.filename, meta), imports, structs, tasks, workflow)
def document(self, items, meta): imports = [] tasks = [] workflow = None for item in items: if isinstance(item, D.Task): tasks.append(item) elif isinstance(item, D.Workflow): if workflow is not None: raise Err.MultipleDefinitions( sp(self.filename, meta), "Document has multiple workflows" ) workflow = item elif isinstance(item, lark.Tree) and item.data == "version": pass elif isinstance(item, dict) and "import" in item: imports.append(item["import"]) else: assert False return D.Document(sp(self.filename, meta), imports, tasks, workflow, self.imported)
def parse_document(txt: str, version: Optional[str] = None, uri: str = "") -> D.Document: if version is None: # for now assume the version is 1.0 if the first line is "version <number>" # otherwise draft-2 version = "draft-2" for line in txt.split("\n"): line = line.strip() if line and line[0] != "#": if line.startswith("version ") and line[8].isdigit(): version = "1.0" break if not txt.strip(): return D.Document( SourcePosition(filename=uri, line=0, column=0, end_line=0, end_column=0), [], {}, [], None, ) try: return _DocTransformer(uri).transform(parse(txt, "document", version)) except lark.exceptions.UnexpectedInput as exn: pos = SourcePosition( filename=(uri if uri != "" else "(buffer)"), line=getattr(exn, "line", "?"), column=getattr(exn, "column", "?"), end_line=getattr(exn, "line", "?"), end_column=getattr(exn, "column", "?"), ) raise Err.SyntaxError(pos, str(exn)) from None except lark.exceptions.VisitError as exn: raise exn.__context__