def testLocalComponentBad(self): script = """ component B() in -> out { e:E() .in > e.in e.out > .out } component E() in -> out { f:std.Identity() .in > f.token f.token > .out } a:std.Counter() b:B() c:io.StandardOut() a.integer > b.in b.out > c.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]["reason"], "Unknown actor type: 'E'") self.assertEqual(len(warnings), 0)
def testLocalComponentRecurse(self): script = """ component E() in -> out { f:std.Identity() .in > f.token f.token > .out } component B() in -> out { e:E() .in > e.in e.out > .out } a:std.Counter() b:B() c:io.StandardOut() a.integer > b.in b.out > c.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testLocalComponentRecurse(self): script = """ component E() in -> out { f:std.Identity() in > f.token f.token > out } component B() in -> out { e:E() in > e.in e.out > out } a:std.Counter() b:B() c:io.StandardOut() a.integer > b.in b.out > c.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testLocalComponentBad(self): script = """ component B() in -> out { e:E() .in > e.in e.out > .out } component E() in -> out { f:std.Identity() .in > f.token f.token > .out } a:std.Counter() b:B() c:io.StandardOut() a.integer > b.in b.out > c.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]['reason'], "Unknown actor type: 'E'") self.assertEqual(len(warnings), 0)
def testCheckSimpleScript(self): script = """ a:Foo() b:Bar() """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertTrue(errors)
def testCheckSimpleScript(self): script = """ a:Foo() b:Bar() """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertTrue(errors)
def testCheckInportConnections(self): script = """ c:io.StandardOut() """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(errors[0]['reason'], "Missing connection to inport 'c.token'") self.assertFalse(warnings)
def testCheckInportConnections(self): script = """ c:io.StandardOut() """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(errors[0]["reason"], "Missing connection to inport 'c.token'") self.assertFalse(warnings)
def testUndefinedActors(self): script = """ a.token > b.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Undefined actor: 'a'") self.assertEqual(errors[1]['reason'], "Undefined actor: 'b'")
def testUndefinedActors(self): script = """ a.token > b.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]["reason"], "Undefined actor: 'a'") self.assertEqual(errors[1]["reason"], "Undefined actor: 'b'")
def testLiteralOnPort(self): script = """ snk : io.StandardOut() 42 > snk.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testUndefinedArguments(self): script = """ a:std.Constant() b:io.StandardOut() a.token > b.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]["reason"], "Missing argument: 'data'")
def testLiteralOnPort(self): script = """ snk : io.StandardOut() 42 > snk.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testUndefinedArguments(self): script = """ a:std.Constant() b:io.StandardOut() a.token > b.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]['reason'], "Missing argument: 'data'")
def testBadComponent7(self): script = """ component Foo() in -> out { .in > .out } """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]['reason'], "Component Foo passes port 'in' directly to port 'out'") self.assertEqual(len(warnings), 0)
def testUndefinedActorInComponent(self): script = """ component Bug() -> out { b.out > .out } """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0)
def testCheckOutportConnections(self): script = """ a:std.CountTimer() b:std.CountTimer() c:io.StandardOut() a.integer > c.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(errors[0]['reason'], "Actor b (std.CountTimer) is missing connection to outport 'integer'") self.assertFalse(warnings)
def testUndefinedActorInComponent(self): script = """ component Bug() -> out { b.out > .out } """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0)
def testBadComponent7(self): script = """ component Foo() in -> out { .in > .out } """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]["reason"], "Component Foo passes port 'in' directly to port 'out'") self.assertEqual(len(warnings), 0)
def testComponentArgumentOnPort(self): script = """ component Foo(foo) -> out { foo > .out } """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testMultipleOutports(self): script = """ a:std.CountTimer() b:io.StandardOut() c:io.StandardOut() a.integer > b.token a.integer > c.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 0)
def testComponentArgumentOnPort(self): script = """ component Foo(foo) -> out { foo > .out } """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testCheckOutportConnections(self): script = """ a:std.CountTimer() b:std.CountTimer() c:io.StandardOut() a.integer > c.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(errors[0]["reason"], "Actor b (std.CountTimer) is missing connection to outport 'integer'") self.assertFalse(warnings)
def testUndefinedConstant(self): script = """ src : std.Constant(data=FOO) snk : io.StandardOut() src.token > snk.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0) self.assertEqual(errors[0]['reason'], "Undefined identifier: 'FOO'")
def testDefinedConstant(self): script = """ define FOO = 42 src : std.Constant(data=FOO) snk : io.StandardOut() src.token > snk.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testUndefinedConstant(self): script = """ src : std.Constant(data=FOO) snk : io.StandardOut() src.token > snk.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0) self.assertEqual(errors[0]["reason"], "Undefined identifier: 'FOO'")
def testCheckInportConnections(self): script = """ a:std.CountTimer() b:std.CountTimer() c:io.StandardOut() a.integer > c.token b.integer > c.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 0) self.assertFalse(warnings)
def testDefinedConstant(self): script = """ define FOO = 42 src : std.Constant(data=FOO) snk : io.StandardOut() src.token > snk.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testCheckInportConnections(self): script = """ a:std.CountTimer() b:std.CountTimer() c:io.StandardOut() a.integer > c.token b.integer > c.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]["reason"], "Actor c (io.StandardOut) has multiple connections to inport 'token'") self.assertFalse(warnings)
def testCheckInportConnections(self): script = """ a:std.CountTimer() b:std.CountTimer() c:io.StandardOut() a.integer > c.token b.integer > c.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Actor c (io.StandardOut) has multiple connections to inport 'token'") self.assertFalse(warnings)
def testComponentUndefinedArgument(self): script = """ component Foo(file) in -> { a:io.StandardOut() .in > a.token } b:Foo() a:std.CountTimer() a.integer > b.in """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]['reason'], "Missing argument: 'file'")
def testCheckLocalComponent(self): script = """ component Foo() -> out { f:std.CountTimer() f.integer > .out } a:Foo() b:io.StandardOut() a.out > b.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertFalse(errors, "\n".join([str(error) for error in errors])) self.assertFalse(warnings, "\n".join([str(warning) for warning in warnings]))
def testBadLocalPort(self): script = """ component Foo() in -> { snk : io.StandardOut() .in > snk.token } src : std.Counter() src.integer > .in """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertNotEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testRedfineInstance(self): script = """ i:std.Identity() src:std.CountTimer() dst:io.StandardOut() i:std.Delay() src.integer > i.token i.token > dst.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0)
def testCheckLocalComponent(self): script = """ component Foo() -> out { f:std.CountTimer() f.integer > .out } a:Foo() b:io.StandardOut() a.out > b.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertFalse(errors, '\n'.join([str(error) for error in errors])) self.assertFalse(warnings, '\n'.join([str(warning) for warning in warnings]))
def compile(source_text, filename=''): # Steps taken: # 1) parser .calvin file -> IR. May produce syntax errors/warnings # 2) checker IR -> IR. May produce syntax errors/warnings # 3) analyzer IR -> app. Should not fail. Sets 'valid' property of IR to True/False deployable = {'valid': False, 'actors': {}, 'connections': {}} ir, errors, warnings = calvin_parser(source_text, filename) # If there were errors during parsing no IR will be generated if not errors: c_errors, c_warnings = check(ir) errors.extend(c_errors) warnings.extend(c_warnings) deployable = generate_app_info(ir) return deployable, errors, warnings
def testRedfineInstance(self): script = """ i:std.Identity() src:std.CountTimer() dst:io.StandardOut() i:std.RecTimer() src.integer > i.token i.token > dst.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0)
def testBadLocalPort(self): script = """ component Foo() in -> { snk : io.StandardOut() .in > snk.token } src : std.Counter() src.integer > .in """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) print errors self.assertNotEqual(len(errors), 0) self.assertEqual(len(warnings), 0)
def testBadComponent4(self): script = """ component Foo() in -> { a:io.StandardOut() } b:Foo() a:std.CountTimer() a.integer > b.in """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Component Foo is missing connection to inport 'in'") self.assertEqual(errors[1]['reason'], "Actor a (io.StandardOut) is missing connection to inport 'token'") self.assertFalse(warnings)
def testBadComponent4(self): script = """ component Foo() in -> { a:io.StandardOut() } b:Foo() a:std.CountTimer() a.integer > b.in """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]["reason"], "Component Foo is missing connection to inport 'in'") self.assertEqual(errors[1]["reason"], "Actor a (io.StandardOut) is missing connection to inport 'token'") self.assertFalse(warnings)
def testComponentUnusedArgument(self): script = """ component Foo(file) in -> { a:io.StandardOut() .in > a.token } b:Foo(file="Foo.txt") a:std.CountTimer() a.integer > b.in """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 0) self.assertEqual(errors[0]["reason"], "Unused argument: 'file'")
def testBadComponent2(self): script = """ component Foo() -> out { a:std.CountTimer() b:io.StandardOut() a.integer > b.token } a:Foo() b:io.StandardOut() a.out > b.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]["reason"], "Component Foo is missing connection to outport 'out'") self.assertFalse(warnings)
def check_script(file): try: with open(file, 'r') as source: source_text = source.read() except: return {}, [{'reason': 'File not found', 'line': 0, 'col': 0}], [] # Steps taken: # 1) parser .calvin file -> IR. May produce syntax errors/warnings # 2) checker IR -> IR. May produce syntax errors/warnings ir, errors, warnings = calvin_parser(source_text, file) # If there were errors during parsing no IR will be generated if not errors: c_errors, c_warnings = check(ir) errors.extend(c_errors) warnings.extend(c_warnings) return ir, errors, warnings
def testBadComponent2(self): script = """ component Foo() -> out { a:std.CountTimer() b:io.StandardOut() a.integer > b.token } a:Foo() b:io.StandardOut() a.out > b.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]['reason'], "Component Foo is missing connection to outport 'out'") self.assertFalse(warnings)
def testBadComponent5(self): script = """ component Foo() in -> { a:io.StandardOut() .foo > a.token } b:Foo() a:std.CountTimer() a.integer > b.in """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Component Foo has no inport 'foo'") self.assertEqual(errors[1]['reason'], "Component Foo is missing connection to inport 'in'") self.assertEqual(len(warnings), 0)
def testNoSuchPort(self): script = """ i:std.Identity() src:std.CountTimer() dst:io.StandardOut() src.integer > i.foo i.bar > dst.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 4) self.assertEqual(errors[0]["reason"], "Actor i (std.Identity) has no inport 'foo'") self.assertEqual(errors[1]["reason"], "Actor i (std.Identity) has no outport 'bar'") self.assertEqual(errors[2]["reason"], "Actor i (std.Identity) is missing connection to inport 'token'") self.assertEqual(errors[3]["reason"], "Actor i (std.Identity) is missing connection to outport 'token'") self.assertEqual(len(warnings), 0)
def testBadComponent6(self): script = """ component Foo() -> out { a:std.CountTimer() a.integer > .foo } b:Foo() a:io.StandardOut() b.out > a.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Component Foo has no outport 'foo'") self.assertEqual(errors[1]['reason'], "Component Foo is missing connection to outport 'out'") self.assertEqual(len(warnings), 0)
def testNoSuchPort(self): script = """ i:std.Identity() src:std.CountTimer() dst:io.StandardOut() src.integer > i.foo i.bar > dst.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 4) self.assertEqual(errors[0]['reason'], "Actor i (std.Identity) has no inport 'foo'") self.assertEqual(errors[1]['reason'], "Actor i (std.Identity) has no outport 'bar'") self.assertEqual(errors[2]['reason'], "Actor i (std.Identity) is missing connection to inport 'token'") self.assertEqual(errors[3]['reason'], "Actor i (std.Identity) is missing connection to outport 'token'") self.assertEqual(len(warnings), 0)
def testBadComponent6(self): script = """ component Foo() -> out { a:std.CountTimer() a.integer > .foo } b:Foo() a:io.StandardOut() b.out > a.token """ result = self.invoke_parser_assert_syntax("inline", script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]["reason"], "Component Foo has no outport 'foo'") self.assertEqual(errors[1]["reason"], "Component Foo is missing connection to outport 'out'") self.assertEqual(len(warnings), 0)
def testBadComponent3(self): script = """ component Foo() -> out { a:std.CountTimer() a.integer > .out a.integer > .out } a:Foo() b:io.StandardOut() a.out > b.token """ result = self.invoke_parser_assert_syntax('inline', script) errors, warnings = check(result) self.assertEqual(len(errors), 2) self.assertEqual(errors[0]['reason'], "Component Foo has multiple connections to outport 'out'") self.assertEqual(errors[1]['reason'], "Component Foo has multiple connections to outport 'out'") self.assertFalse(warnings)
def compile(source_text, filename='', verify=True): # Steps taken: # 1) parser .calvin file -> IR. May produce syntax errors/warnings # 2) checker IR -> IR. May produce syntax errors/warnings # 3) analyzer IR -> app. Should not fail. Sets 'valid' property of IR to True/False deployable = {'valid': False, 'actors': {}, 'connections': {}} _log.debug("Parsing...") ir, errors, warnings = calvin_parser(source_text, filename) _log.debug("Parsed %s, %s, %s" % (ir, errors, warnings)) # If there were errors during parsing no IR will be generated if not errors: c_errors, c_warnings = check(ir, verify=verify) errors.extend(c_errors) warnings.extend(c_warnings) deployable = generate_app_info(ir, verify=verify) if errors: deployable['valid'] = False _log.debug("Compiled %s, %s, %s" % (deployable, errors, warnings)) return deployable, errors, warnings