diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-02 15:47:54 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-02 15:47:54 +0000 |
commit | a69d6c57f34ee4f1936cb928a880fc911ef04dbb (patch) | |
tree | d7c16abad04965a624b7c900a4d892ebf84c22ed /mojo | |
parent | 16f78889570aa2cd6846bc89289795a3948750c1 (diff) | |
download | chromium_src-a69d6c57f34ee4f1936cb928a880fc911ef04dbb.zip chromium_src-a69d6c57f34ee4f1936cb928a880fc911ef04dbb.tar.gz chromium_src-a69d6c57f34ee4f1936cb928a880fc911ef04dbb.tar.bz2 |
Mojo: Mojom: Add a Parameter object to the AST.
Also add a basic parser test for interfaces/methods. (I also verified
that the change does not affect the output of the generators on any
.mojom file under mojo/.)
R=jamesr@chromium.org
Review URL: https://codereview.chromium.org/365773002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281040 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
4 files changed, 97 insertions, 14 deletions
diff --git a/mojo/public/tools/bindings/pylib/mojom/parse/ast.py b/mojo/public/tools/bindings/pylib/mojom/parse/ast.py index bd398c1..5a2ad90 100644 --- a/mojo/public/tools/bindings/pylib/mojom/parse/ast.py +++ b/mojo/public/tools/bindings/pylib/mojom/parse/ast.py @@ -23,3 +23,17 @@ class Ordinal(BaseNode): def __eq__(self, other): return self.value == other.value + +class Parameter(BaseNode): + """Represents a method request or response parameter.""" + def __init__(self, typename, name, ordinal, **kwargs): + assert isinstance(ordinal, Ordinal) + BaseNode.__init__(self, **kwargs) + self.typename = typename + self.name = name + self.ordinal = ordinal + + def __eq__(self, other): + return self.typename == other.typename and \ + self.name == other.name and \ + self.ordinal == other.ordinal diff --git a/mojo/public/tools/bindings/pylib/mojom/parse/parser.py b/mojo/public/tools/bindings/pylib/mojom/parse/parser.py index cc5b68c..7add767 100644 --- a/mojo/public/tools/bindings/pylib/mojom/parse/parser.py +++ b/mojo/public/tools/bindings/pylib/mojom/parse/parser.py @@ -183,13 +183,14 @@ class Parser(object): if len(p) == 1: p[0] = [] elif len(p) == 2: - p[0] = _ListFromConcat(p[1]) - elif len(p) > 3: - p[0] = _ListFromConcat(p[1], p[3]) + p[0] = [p[1]] + else: + p[0] = [p[1]] + p[3] def p_parameter(self, p): """parameter : typename NAME ordinal""" - p[0] = ('PARAM', p[1], p[2], p[3]) + p[0] = ast.Parameter(p[1], p[2], p[3], + filename=self.filename, lineno=p.lineno(1)) def p_typename(self, p): """typename : basictypename diff --git a/mojo/public/tools/bindings/pylib/mojom/parse/translate.py b/mojo/public/tools/bindings/pylib/mojom/parse/translate.py index 064b658..6b5f4fc 100644 --- a/mojo/public/tools/bindings/pylib/mojom/parse/translate.py +++ b/mojo/public/tools/bindings/pylib/mojom/parse/translate.py @@ -66,26 +66,27 @@ def _GetAttribute(attributes, name): return out def _MapField(tree): - assert type(tree[3]) is ast.Ordinal + assert isinstance(tree[3], ast.Ordinal) return {'name': tree[2], 'kind': _MapKind(tree[1]), 'ordinal': tree[3].value, 'default': tree[4]} -def _MapParameter(tree): - assert type(tree[3]) is ast.Ordinal - return {'name': tree[2], - 'kind': _MapKind(tree[1]), - 'ordinal': tree[3].value} - def _MapMethod(tree): - assert type(tree[3]) is ast.Ordinal + def ParameterToDict(param): + assert isinstance(param, ast.Parameter) + return {'name': param.name, + 'kind': _MapKind(param.typename), + 'ordinal': param.ordinal.value} + + assert isinstance(tree[2], list) + assert isinstance(tree[3], ast.Ordinal) method = {'name': tree[1], - 'parameters': _MapTree(_MapParameter, tree[2], 'PARAM'), + 'parameters': map(ParameterToDict, tree[2]), 'ordinal': tree[3].value} # Note: |tree[4]| may be an empty list, indicating a parameter-less response. if tree[4] is not None: - method['response_parameters'] = _MapTree(_MapParameter, tree[4], 'PARAM') + method['response_parameters'] = map(ParameterToDict, tree[4]) return method def _MapEnumField(tree): diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py index 6d114bf..7e47994 100644 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py +++ b/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py @@ -560,5 +560,72 @@ struct MyStruct { r"^my_file\.mojom:2: Error: Unexpected 'abcdefg':"): parser.Parse(source3, "my_file.mojom") + def testValidMethod(self): + """Tests parsing method declarations.""" + source1 = """\ +interface MyInterface { + MyMethod(int32 a); +}; +""" + expected1 = \ +[('MODULE', + '', + None, + [('INTERFACE', + 'MyInterface', + None, + [('METHOD', + 'MyMethod', + [ast.Parameter('int32', 'a', ast.Ordinal(None))], + ast.Ordinal(None), + None)])])] + self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) + + source2 = """\ +interface MyInterface { + MyMethod1@0(int32 a@0, int64 b@1); + MyMethod2@1() => (); +}; +""" + expected2 = \ +[('MODULE', + '', + None, + [('INTERFACE', + 'MyInterface', + None, + [('METHOD', + 'MyMethod1', + [ast.Parameter('int32', 'a', ast.Ordinal(0)), + ast.Parameter('int64', 'b', ast.Ordinal(1))], + ast.Ordinal(0), + None), + ('METHOD', + 'MyMethod2', + [], + ast.Ordinal(1), + [])])])] + self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) + + source3 = """\ +interface MyInterface { + MyMethod(string a) => (int32 a, bool b); +}; +""" + expected3 = \ +[('MODULE', + '', + None, + [('INTERFACE', + 'MyInterface', + None, + [('METHOD', + 'MyMethod', + [ast.Parameter('string', 'a', ast.Ordinal(None))], + ast.Ordinal(None), + [ast.Parameter('int32', 'a', ast.Ordinal(None)), + ast.Parameter('bool', 'b', ast.Ordinal(None))])])])] + self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) + if __name__ == "__main__": unittest.main() |