blob: ed48a5329fc0eac13cb6a2b6ea96aec0817c31c3 [file] [log] [blame]
David Brazdil2c27f2c2015-05-12 18:06:38 +01001# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from common.logger import Logger
David Brazdilb34c35e2015-08-20 11:46:04 +010016from file_format.checker.struct import TestExpression, TestAssertion
David Brazdil2c27f2c2015-05-12 18:06:38 +010017
18import re
19
David Brazdil4e9aac12015-05-18 17:45:17 +010020def headAndTail(list):
21 return list[0], list[1:]
David Brazdil2c27f2c2015-05-12 18:06:38 +010022
David Brazdil4e9aac12015-05-18 17:45:17 +010023def splitAtSeparators(expressions):
David Brazdilb34c35e2015-08-20 11:46:04 +010024 """ Splits a list of TestExpressions at separators. """
David Brazdil4e9aac12015-05-18 17:45:17 +010025 splitExpressions = []
26 wordStart = 0
27 for index, expression in enumerate(expressions):
David Brazdilb34c35e2015-08-20 11:46:04 +010028 if expression.variant == TestExpression.Variant.Separator:
David Brazdil4e9aac12015-05-18 17:45:17 +010029 splitExpressions.append(expressions[wordStart:index])
30 wordStart = index + 1
31 splitExpressions.append(expressions[wordStart:])
32 return splitExpressions
David Brazdil2c27f2c2015-05-12 18:06:38 +010033
David Brazdilb34c35e2015-08-20 11:46:04 +010034def getVariable(name, variables, pos):
35 if name in variables:
36 return variables[name]
37 else:
David Brazdil0f73bda2016-03-02 16:09:46 +000038 Logger.testFailed("Missing definition of variable \"{}\"".format(name), pos, variables)
David Brazdilb34c35e2015-08-20 11:46:04 +010039
40def setVariable(name, value, variables, pos):
41 if name not in variables:
42 return variables.copyWith(name, value)
43 else:
David Brazdil0f73bda2016-03-02 16:09:46 +000044 Logger.testFailed("Multiple definitions of variable \"{}\"".format(name), pos, variables)
David Brazdilb34c35e2015-08-20 11:46:04 +010045
David Brazdil4e9aac12015-05-18 17:45:17 +010046def matchWords(checkerWord, stringWord, variables, pos):
David Brazdilb34c35e2015-08-20 11:46:04 +010047 """ Attempts to match a list of TestExpressions against a string.
David Brazdil4e9aac12015-05-18 17:45:17 +010048 Returns updated variable dictionary if successful and None otherwise.
David Brazdil2c27f2c2015-05-12 18:06:38 +010049 """
David Brazdil4e9aac12015-05-18 17:45:17 +010050 for expression in checkerWord:
51 # If `expression` is a variable reference, replace it with the value.
David Brazdilb34c35e2015-08-20 11:46:04 +010052 if expression.variant == TestExpression.Variant.VarRef:
53 pattern = re.escape(getVariable(expression.name, variables, pos))
David Brazdil4e9aac12015-05-18 17:45:17 +010054 else:
David Brazdilb34c35e2015-08-20 11:46:04 +010055 pattern = expression.text
David Brazdil2c27f2c2015-05-12 18:06:38 +010056
David Brazdil4e9aac12015-05-18 17:45:17 +010057 # Match the expression's regex pattern against the remainder of the word.
58 # Note: re.match will succeed only if matched from the beginning.
59 match = re.match(pattern, stringWord)
60 if not match:
61 return None
David Brazdil2c27f2c2015-05-12 18:06:38 +010062
David Brazdil4e9aac12015-05-18 17:45:17 +010063 # If `expression` was a variable definition, set the variable's value.
David Brazdilb34c35e2015-08-20 11:46:04 +010064 if expression.variant == TestExpression.Variant.VarDef:
65 variables = setVariable(expression.name, stringWord[:match.end()], variables, pos)
David Brazdil2c27f2c2015-05-12 18:06:38 +010066
David Brazdil4e9aac12015-05-18 17:45:17 +010067 # Move cursor by deleting the matched characters.
68 stringWord = stringWord[match.end():]
69
70 # Make sure the entire word matched, i.e. `stringWord` is empty.
71 if stringWord:
72 return None
David Brazdilc4de9432015-05-20 11:03:22 +010073
David Brazdil4e9aac12015-05-18 17:45:17 +010074 return variables
75
76def MatchLines(checkerLine, stringLine, variables):
77 """ Attempts to match a CHECK line against a string. Returns variable state
78 after the match if successful and None otherwise.
79 """
David Brazdilb34c35e2015-08-20 11:46:04 +010080 assert checkerLine.variant != TestAssertion.Variant.Eval
81
David Brazdil4e9aac12015-05-18 17:45:17 +010082 checkerWords = splitAtSeparators(checkerLine.expressions)
83 stringWords = stringLine.split()
84
85 while checkerWords:
David Brazdilb34c35e2015-08-20 11:46:04 +010086 # Get the next run of TestExpressions which must match one string word.
David Brazdil4e9aac12015-05-18 17:45:17 +010087 checkerWord, checkerWords = headAndTail(checkerWords)
88
89 # Keep reading words until a match is found.
90 wordMatched = False
91 while stringWords:
92 stringWord, stringWords = headAndTail(stringWords)
93 newVariables = matchWords(checkerWord, stringWord, variables, checkerLine)
94 if newVariables is not None:
95 wordMatched = True
96 variables = newVariables
97 break
98 if not wordMatched:
99 return None
100
David Brazdilb34c35e2015-08-20 11:46:04 +0100101 # All TestExpressions matched. Return new variable state.
David Brazdil4e9aac12015-05-18 17:45:17 +0100102 return variables
David Brazdilb34c35e2015-08-20 11:46:04 +0100103
104def getEvalText(expression, variables, pos):
105 if expression.variant == TestExpression.Variant.PlainText:
106 return expression.text
107 else:
108 assert expression.variant == TestExpression.Variant.VarRef
109 return getVariable(expression.name, variables, pos)
110
111def EvaluateLine(checkerLine, variables):
112 assert checkerLine.variant == TestAssertion.Variant.Eval
113 eval_string = "".join(map(lambda expr: getEvalText(expr, variables, checkerLine),
114 checkerLine.expressions))
115 return eval(eval_string)