Robot Framework
parser.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from ..lexer import Token, get_tokens, get_resource_tokens, get_init_tokens
17 from ..model import Statement, ModelVisitor
18 
19 from .fileparser import FileParser
20 
21 
22 
49 def get_model(source, data_only=False, curdir=None, lang=None):
50  return _get_model(get_tokens, source, data_only, curdir, lang)
51 
52 
53 
58 def get_resource_model(source, data_only=False, curdir=None, lang=None):
59  return _get_model(get_resource_tokens, source, data_only, curdir, lang)
60 
61 
62 
68 def get_init_model(source, data_only=False, curdir=None, lang=None):
69  return _get_model(get_init_tokens, source, data_only, curdir, lang)
70 
71 
72 def _get_model(token_getter, source, data_only=False, curdir=None, lang=None):
73  tokens = token_getter(source, data_only, lang=lang)
74  statements = _tokens_to_statements(tokens, curdir)
75  model = _statements_to_model(statements, source)
76  model.validate_model()
77  return model
78 
79 
80 def _tokens_to_statements(tokens, curdir=None):
81  statement = []
82  EOS = Token.EOS
83  for t in tokens:
84  if curdir and '${CURDIR}' in t.value:
85  t.value = t.value.replace('${CURDIR}', curdir)
86  if t.type != EOS:
87  statement.append(t)
88  else:
89  yield Statement.from_tokens(statement)
90  statement = []
91 
92 
93 def _statements_to_model(statements, source=None):
94  parser = FileParser(source=source)
95  model = parser.model
96  stack = [parser]
97  for statement in statements:
98  while not stack[-1].handles(statement):
99  stack.pop()
100  parser = stack[-1].parse(statement)
101  if parser:
102  stack.append(parser)
103  # Implicit comment sections have no header.
104  if model.sections and model.sections[0].header is None:
105  SetLanguages(model).visit(model.sections[0])
106  return model
107 
108 
110 
111  def __init__(self, file):
112  self.filefile = file
113 
114  def visit_Config(self, node):
115  self.filefile.languages += (node.language.code,)
NodeVisitor that supports matching nodes based on their base classes.
Definition: visitor.py:45
def get_model(source, data_only=False, curdir=None, lang=None)
Parses the given source to a model represented as an AST.
Definition: parser.py:49
def _statements_to_model(statements, source=None)
Definition: parser.py:93
def get_resource_model(source, data_only=False, curdir=None, lang=None)
Parses the given source to a resource file model.
Definition: parser.py:58
def _tokens_to_statements(tokens, curdir=None)
Definition: parser.py:80
def _get_model(token_getter, source, data_only=False, curdir=None, lang=None)
Definition: parser.py:72
def get_init_model(source, data_only=False, curdir=None, lang=None)
Parses the given source to a init file model.
Definition: parser.py:68