hotspot/src/jdk.internal.vm.compiler/.mx.graal/outputparser.py
changeset 45271 81a039ba9344
parent 43972 1ade39b8381b
equal deleted inserted replaced
45270:bc1a5b8902d3 45271:81a039ba9344
       
     1 # ----------------------------------------------------------------------------------------------------
       
     2 #
       
     3 # Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
       
     4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     5 #
       
     6 # This code is free software; you can redistribute it and/or modify it
       
     7 # under the terms of the GNU General Public License version 2 only, as
       
     8 # published by the Free Software Foundation.
       
     9 #
       
    10 # This code is distributed in the hope that it will be useful, but WITHOUT
       
    11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13 # version 2 for more details (a copy is included in the LICENSE file that
       
    14 # accompanied this code).
       
    15 #
       
    16 # You should have received a copy of the GNU General Public License version
       
    17 # 2 along with this work; if not, write to the Free Software Foundation,
       
    18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19 #
       
    20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21 # or visit www.oracle.com if you need additional information or have any
       
    22 # questions.
       
    23 #
       
    24 # ----------------------------------------------------------------------------------------------------
       
    25 
       
    26 import re
       
    27 
       
    28 class OutputParser:
       
    29 
       
    30     def __init__(self):
       
    31         self.matchers = []
       
    32 
       
    33     def addMatcher(self, matcher):
       
    34         self.matchers.append(matcher)
       
    35 
       
    36     def parse(self, output):
       
    37         valueMaps = []
       
    38         for matcher in self.matchers:
       
    39             matcher.parse(output, valueMaps)
       
    40         return valueMaps
       
    41 
       
    42 """
       
    43 Produces a value map for each match of a given regular expression
       
    44 in some text. The value map is specified by a template map
       
    45 where each key and value in the template map is either a constant
       
    46 value or a named group in the regular expression. The latter is
       
    47 given as the group name enclosed in '<' and '>'.
       
    48 """
       
    49 class ValuesMatcher:
       
    50 
       
    51     def __init__(self, regex, valuesTemplate):
       
    52         assert isinstance(valuesTemplate, dict)
       
    53         self.regex = regex
       
    54         self.valuesTemplate = valuesTemplate
       
    55 
       
    56     def parse(self, text, valueMaps):
       
    57         for match in self.regex.finditer(text):
       
    58             valueMap = {}
       
    59             for keyTemplate, valueTemplate in self.valuesTemplate.items():
       
    60                 key = self.get_template_value(match, keyTemplate)
       
    61                 value = self.get_template_value(match, valueTemplate)
       
    62                 assert not valueMap.has_key(key), key
       
    63                 valueMap[key] = value
       
    64             valueMaps.append(valueMap)
       
    65 
       
    66     def get_template_value(self, match, template):
       
    67         def replace_var(m):
       
    68             groupName = m.group(1)
       
    69             return match.group(groupName)
       
    70 
       
    71         return re.sub(r'<([\w]+)>', replace_var, template)