Mojo: Mojom: Make specialized handle types (e.g., message_pipe) not keywords.

Other types (e.g., int32) aren't keywords, so it's a bit anomalous that
the specialized handle types are. (Insisting that they be keywords makes
adding future handle types more difficult, since doing so could break
existing .mojom files.)

R=darin@chromium.org

Review URL: https://codereview.chromium.org/295773002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271519 0039d316-1c4b-4281-b951-d872f2087c98
parent 4f2a7597
......@@ -57,10 +57,6 @@ class Lexer(object):
##
keywords = (
'HANDLE',
'DATA_PIPE_CONSUMER',
'DATA_PIPE_PRODUCER',
'MESSAGE_PIPE',
'SHARED_BUFFER',
'IMPORT',
'MODULE',
......
......@@ -193,20 +193,25 @@ class Parser(object):
def p_basictypename(self, p):
"""basictypename : identifier
| HANDLE
| specializedhandle"""
| handletype"""
p[0] = p[1]
def p_specializedhandle(self, p):
"""specializedhandle : HANDLE LANGLE specializedhandlename RANGLE"""
p[0] = "handle<" + p[3] + ">"
def p_specializedhandlename(self, p):
"""specializedhandlename : DATA_PIPE_CONSUMER
| DATA_PIPE_PRODUCER
| MESSAGE_PIPE
| SHARED_BUFFER"""
p[0] = p[1]
def p_handletype(self, p):
"""handletype : HANDLE
| HANDLE LANGLE identifier RANGLE"""
if len(p) == 2:
p[0] = p[1]
else:
if p[3] not in ('data_pipe_consumer',
'data_pipe_producer',
'message_pipe',
'shared_buffer'):
# Note: We don't enable tracking of line numbers for everything, so we
# can't use |p.lineno(3)|.
raise ParseError(self.filename, "Invalid handle type %r:" % p[3],
lineno=p.lineno(1),
snippet=self._GetSnippet(p.lineno(1)))
p[0] = "handle<" + p[3] + ">"
def p_array(self, p):
"""array : typename LBRACKET RBRACKET"""
......
......@@ -67,12 +67,6 @@ class LexerTest(unittest.TestCase):
"""Tests valid keywords."""
self.assertEquals(self._SingleTokenForInput("handle"),
_MakeLexTokenForKeyword("handle"))
self.assertEquals(self._SingleTokenForInput("data_pipe_consumer"),
_MakeLexTokenForKeyword("data_pipe_consumer"))
self.assertEquals(self._SingleTokenForInput("data_pipe_producer"),
_MakeLexTokenForKeyword("data_pipe_producer"))
self.assertEquals(self._SingleTokenForInput("message_pipe"),
_MakeLexTokenForKeyword("message_pipe"))
self.assertEquals(self._SingleTokenForInput("import"),
_MakeLexTokenForKeyword("import"))
self.assertEquals(self._SingleTokenForInput("module"),
......
......@@ -311,7 +311,7 @@ struct MyStruct {
parser.Parse(source6, "my_file.mojom")
def testNestedNamespace(self):
"""Tests nested namespaces work."""
"""Tests that "nested" namespaces work."""
source = """\
module my.mod {
......@@ -331,6 +331,47 @@ struct MyStruct {
[('FIELD', 'int32', 'a', ast.Ordinal(None), None)])])]
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testValidHandleTypes(self):
"""Tests (valid) handle types."""
source = """\
struct MyStruct {
handle a;
handle<data_pipe_consumer> b;
handle <data_pipe_producer> c;
handle < message_pipe > d;
handle
< shared_buffer
> e;
};
"""
expected = \
[('MODULE',
'',
None,
[('STRUCT',
'MyStruct',
None,
[('FIELD', 'handle', 'a', ast.Ordinal(None), None),
('FIELD', 'handle<data_pipe_consumer>', 'b', ast.Ordinal(None), None),
('FIELD', 'handle<data_pipe_producer>', 'c', ast.Ordinal(None), None),
('FIELD', 'handle<message_pipe>', 'd', ast.Ordinal(None), None),
('FIELD', 'handle<shared_buffer>', 'e', ast.Ordinal(None), None)])])]
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testInvalidHandleType(self):
"""Tests an invalid (unknown) handle type."""
source = """\
struct MyStruct {
handle<wtf_is_this> foo;
};
"""
with self.assertRaisesRegexp(
parser.ParseError,
r"^my_file\.mojom:2: Error: "
r"Invalid handle type 'wtf_is_this':\n"
r" handle<wtf_is_this> foo;$"):
parser.Parse(source, "my_file.mojom")
if __name__ == "__main__":
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment