ThriftProgramParser implements ProgramParser { @Override public ProgramType parse(InputStream in, File file, Collection<File> includeDirs) throws IOException { try { return parseInternal(in, file, includeDirs); } catch (TokenizerException e) { if (e.getFile() == null) { e.setFile(file.getName()); } throw e; } } ThriftProgramParser();  ThriftProgramParser(boolean requireFieldId,
                               boolean requireEnumValue);  ThriftProgramParser(boolean requireFieldId,
                               boolean requireEnumValue,
                               boolean allowLanguageReservedNames); @Override ProgramType parse(InputStream in, File file, Collection<File> includeDirs); static final Pattern VALID_NAMESPACE; static final Pattern VALID_SDI_NAMESPACE; }
@Test public void testParse_calculator() throws IOException, ParseException { copyResourceTo("/parser/calculator/number.thrift", tmp.getRoot()); copyResourceTo("/parser/calculator/calculator.thrift", tmp.getRoot()); File calculator = new File(tmp.getRoot(), "calculator.thrift"); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(calculator), calculator, new TreeSet<>()); assertEquals( "{\n" + " program_name = \"calculator\"\n" + " includes = [\n" + " \"number.thrift\"\n" + " ]\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.calculator\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_enum = {\n" + " documentation = \"Block comment on type.\"\n" + " name = \"Operator\"\n" + " values = [\n" + " {\n" + " name = \"IDENTITY\"\n" + " id = 1\n" + " start_pos = {\n" + " line_no = 10\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 10\n" + " line_pos = 17\n" + " }\n" + " },\n" + " {\n" + " documentation = \"Block comment on enum.\"\n" + " name = \"ADD\"\n" + " id = 2\n" + " start_pos = {\n" + " line_no = 14\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 14\n" + " line_pos = 8\n" + " }\n" + " },\n" + " {\n" + " name = \"SUBTRACT\"\n" + " id = 3\n" + " start_pos = {\n" + " line_no = 15\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 15\n" + " line_pos = 13\n" + " }\n" + " },\n" + " {\n" + " name = \"MULTIPLY\"\n" + " id = 4\n" + " start_pos = {\n" + " line_no = 16\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 16\n" + " line_pos = 13\n" + " }\n" + " },\n" + " {\n" + " name = \"DIVIDE\"\n" + " id = 5\n" + " start_pos = {\n" + " line_no = 17\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 17\n" + " line_pos = 11\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 8\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 18\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " variant = UNION\n" + " name = \"Operand\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " type = \"Operation\"\n" + " name = \"operation\"\n" + " start_pos = {\n" + " line_no = 24\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 24\n" + " line_pos = 27\n" + " }\n" + " },\n" + " {\n" + " documentation = \"Block comment\\n - with formatting.\\nOn field.\"\n" + " id = 2\n" + " type = \"double\"\n" + " name = \"number\"\n" + " start_pos = {\n" + " line_no = 30\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 30\n" + " line_pos = 21\n" + " }\n" + " },\n" + " {\n" + " id = 3\n" + " type = \"number.Imaginary\"\n" + " name = \"imaginary\"\n" + " start_pos = {\n" + " line_no = 31\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 31\n" + " line_pos = 34\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 21\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 32\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " name = \"Operation\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " type = \"Operator\"\n" + " name = \"operator\"\n" + " start_pos = {\n" + " line_no = 35\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 35\n" + " line_pos = 25\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " type = \"list<Operand>\"\n" + " name = \"operands\"\n" + " start_pos = {\n" + " line_no = 36\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 36\n" + " line_pos = 30\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"compact\": \"\"\n" + " }\n" + " start_pos = {\n" + " line_no = 34\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 37\n" + " line_pos = 17\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " variant = EXCEPTION\n" + " name = \"CalculateException\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " requirement = REQUIRED\n" + " type = \"string\"\n" + " name = \"message\"\n" + " start_pos = {\n" + " line_no = 40\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 40\n" + " line_pos = 31\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " type = \"Operation\"\n" + " name = \"operation\"\n" + " start_pos = {\n" + " line_no = 41\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 41\n" + " line_pos = 27\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 39\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 42\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_service = {\n" + " name = \"BaseCalculator\"\n" + " methods = [\n" + " {\n" + " one_way = true\n" + " name = \"iamalive\"\n" + " params = []\n" + " start_pos = {\n" + " line_no = 46\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 46\n" + " line_pos = 27\n" + " }\n" + " },\n" + " {\n" + " return_type = \"i32\"\n" + " name = \"numCalls\"\n" + " params = [\n" + " {\n" + " id = 1\n" + " requirement = REQUIRED\n" + " type = \"i32\"\n" + " name = \"minus\"\n" + " start_pos = {\n" + " line_no = 47\n" + " line_pos = 18\n" + " }\n" + " end_pos = {\n" + " line_no = 47\n" + " line_pos = 39\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " requirement = OPTIONAL\n" + " type = \"i32\"\n" + " name = \"inAdditionTo\"\n" + " start_pos = {\n" + " line_no = 47\n" + " line_pos = 41\n" + " }\n" + " end_pos = {\n" + " line_no = 47\n" + " line_pos = 69\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 47\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 47\n" + " line_pos = 70\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"deprecated\": \"Because reasons\"\n" + " }\n" + " start_pos = {\n" + " line_no = 44\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 48\n" + " line_pos = 35\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_service = {\n" + " documentation = \"Block comment on service\"\n" + " name = \"Calculator\"\n" + " extend = \"BaseCalculator\"\n" + " methods = [\n" + " {\n" + " documentation = \"Block comment on method.\"\n" + " return_type = \"Operand\"\n" + " name = \"calculate\"\n" + " params = [\n" + " {\n" + " id = 1\n" + " type = \"Operation\"\n" + " name = \"op\"\n" + " start_pos = {\n" + " line_no = 57\n" + " line_pos = 23\n" + " }\n" + " end_pos = {\n" + " line_no = 57\n" + " line_pos = 38\n" + " }\n" + " }\n" + " ]\n" + " exceptions = [\n" + " {\n" + " id = 1\n" + " type = \"CalculateException\"\n" + " name = \"ce\"\n" + " start_pos = {\n" + " line_no = 57\n" + " line_pos = 48\n" + " }\n" + " end_pos = {\n" + " line_no = 57\n" + " line_pos = 72\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 57\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 57\n" + " line_pos = 73\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 53\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 58\n" + " line_pos = 1\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_const = {\n" + " documentation = \"Block comment on constant.\"\n" + " type = \"Operand\"\n" + " name = \"PI\"\n" + " value = \"{\\n \\\"number\\\": 3.141592\\n}\"\n" + " value_start_pos = {\n" + " line_no = 63\n" + " line_pos = 20\n" + " }\n" + " start_pos = {\n" + " line_no = 63\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 65\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_const = {\n" + " type = \"set<Operator>\"\n" + " name = \"kComplexOperands\"\n" + " value = \"[\\n Operator.MULTIPLY,\\n Operator.DIVIDE\\n]\"\n" + " value_start_pos = {\n" + " line_no = 68\n" + " line_pos = 40\n" + " }\n" + " start_pos = {\n" + " line_no = 68\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 71\n" + " line_pos = 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}", debugString(program)); }
@Test public void testParse_calculator_strict() throws IOException { copyResourceTo("/parser/calculator/number.thrift", tmp.getRoot()); copyResourceTo("/parser/calculator/calculator_strict.thrift", tmp.getRoot()); File calculator = new File(tmp.getRoot(), "calculator_strict.thrift"); ThriftProgramParser parser = new ThriftProgramParser(true, true); ProgramType program = parser.parse(new FileInputStream(calculator), calculator, new TreeSet<>()); assertEquals( "{\n" + " program_name = \"calculator_strict\"\n" + " includes = [\n" + " \"number.thrift\"\n" + " ]\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.calculator\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_enum = {\n" + " documentation = \"Block comment on type.\"\n" + " name = \"Operator\"\n" + " values = [\n" + " {\n" + " name = \"IDENTITY\"\n" + " id = 1\n" + " start_pos = {\n" + " line_no = 10\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 10\n" + " line_pos = 17\n" + " }\n" + " },\n" + " {\n" + " documentation = \"Block comment on enum.\"\n" + " name = \"ADD\"\n" + " id = 2\n" + " start_pos = {\n" + " line_no = 14\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 14\n" + " line_pos = 12\n" + " }\n" + " },\n" + " {\n" + " name = \"SUBTRACT\"\n" + " id = 3\n" + " start_pos = {\n" + " line_no = 15\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 15\n" + " line_pos = 17\n" + " }\n" + " },\n" + " {\n" + " name = \"MULTIPLY\"\n" + " id = 4\n" + " start_pos = {\n" + " line_no = 16\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 16\n" + " line_pos = 17\n" + " }\n" + " },\n" + " {\n" + " name = \"DIVIDE\"\n" + " id = 5\n" + " start_pos = {\n" + " line_no = 17\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 17\n" + " line_pos = 15\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 8\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 18\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " variant = UNION\n" + " name = \"Operand\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " type = \"Operation\"\n" + " name = \"operation\"\n" + " start_pos = {\n" + " line_no = 24\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 24\n" + " line_pos = 27\n" + " }\n" + " },\n" + " {\n" + " documentation = \"Block comment\\n - with formatting.\\nOn field.\"\n" + " id = 2\n" + " type = \"double\"\n" + " name = \"number\"\n" + " start_pos = {\n" + " line_no = 30\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 30\n" + " line_pos = 21\n" + " }\n" + " },\n" + " {\n" + " id = 3\n" + " type = \"number.Imaginary\"\n" + " name = \"imaginary\"\n" + " start_pos = {\n" + " line_no = 31\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 31\n" + " line_pos = 34\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 21\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 32\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " name = \"Operation\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " type = \"Operator\"\n" + " name = \"operator\"\n" + " start_pos = {\n" + " line_no = 35\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 35\n" + " line_pos = 25\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " type = \"list<Operand>\"\n" + " name = \"operands\"\n" + " start_pos = {\n" + " line_no = 36\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 36\n" + " line_pos = 30\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"compact\": \"\"\n" + " }\n" + " start_pos = {\n" + " line_no = 34\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 37\n" + " line_pos = 17\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " variant = EXCEPTION\n" + " name = \"CalculateException\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " requirement = REQUIRED\n" + " type = \"string\"\n" + " name = \"message\"\n" + " start_pos = {\n" + " line_no = 40\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 40\n" + " line_pos = 31\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " type = \"Operation\"\n" + " name = \"operation\"\n" + " start_pos = {\n" + " line_no = 41\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 41\n" + " line_pos = 27\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 39\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 42\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_service = {\n" + " name = \"Calculator\"\n" + " methods = [\n" + " {\n" + " documentation = \"Block comment on method.\"\n" + " return_type = \"Operand\"\n" + " name = \"calculate\"\n" + " params = [\n" + " {\n" + " id = 1\n" + " type = \"Operation\"\n" + " name = \"op\"\n" + " start_pos = {\n" + " line_no = 48\n" + " line_pos = 23\n" + " }\n" + " end_pos = {\n" + " line_no = 48\n" + " line_pos = 38\n" + " }\n" + " }\n" + " ]\n" + " exceptions = [\n" + " {\n" + " id = 1\n" + " type = \"CalculateException\"\n" + " name = \"ce\"\n" + " start_pos = {\n" + " line_no = 48\n" + " line_pos = 48\n" + " }\n" + " end_pos = {\n" + " line_no = 48\n" + " line_pos = 72\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 48\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 48\n" + " line_pos = 73\n" + " }\n" + " },\n" + " {\n" + " one_way = true\n" + " name = \"iamalive\"\n" + " params = []\n" + " start_pos = {\n" + " line_no = 50\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 50\n" + " line_pos = 27\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 44\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 51\n" + " line_pos = 1\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_const = {\n" + " documentation = \"Block comment on constant.\"\n" + " type = \"Operand\"\n" + " name = \"PI\"\n" + " value = \"{\\n \\\"number\\\": 3.141592\\n}\"\n" + " value_start_pos = {\n" + " line_no = 56\n" + " line_pos = 20\n" + " }\n" + " start_pos = {\n" + " line_no = 56\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 58\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_const = {\n" + " type = \"set<Operator>\"\n" + " name = \"kComplexOperands\"\n" + " value = \"[\\n Operator.MULTIPLY,\\n Operator.DIVIDE\\n]\"\n" + " value_start_pos = {\n" + " line_no = 61\n" + " line_pos = 40\n" + " }\n" + " start_pos = {\n" + " line_no = 61\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 64\n" + " line_pos = 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}", debugString(program)); }
@Test public void testParse_number() throws IOException { copyResourceTo("/parser/calculator/number.thrift", tmp.getRoot()); File number = new File(tmp.getRoot(), "number.thrift"); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(number), number, new TreeSet<>()); assertThat(debugString(program), is(is( "{\n" + " program_name = \"number\"\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.number\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_typedef = {\n" + " type = \"double\"\n" + " name = \"real\"\n" + " start_pos = {\n" + " line_no = 4\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 4\n" + " line_pos = 20\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " name = \"Imaginary\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " requirement = REQUIRED\n" + " type = \"real\"\n" + " name = \"v\"\n" + " start_pos = {\n" + " line_no = 7\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 7\n" + " line_pos = 23\n" + " }\n" + " },\n" + " {\n" + " id = 2\n" + " type = \"double\"\n" + " name = \"i\"\n" + " default_value = \"0.0\"\n" + " value_start_pos = {\n" + " line_no = 8\n" + " line_pos = 19\n" + " }\n" + " start_pos = {\n" + " line_no = 8\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 8\n" + " line_pos = 22\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"compact\": \"true\"\n" + " }\n" + " start_pos = {\n" + " line_no = 6\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 21\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_typedef = {\n" + " type = \"Imaginary\"\n" + " name = \"I\"\n" + " start_pos = {\n" + " line_no = 11\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 11\n" + " line_pos = 20\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_const = {\n" + " type = \"Imaginary\"\n" + " name = \"kSqrtMinusOne\"\n" + " value = \"{\\n \\\"v\\\": 0.0,\\n \\\"i\\\": -1.0\\n}\"\n" + " value_start_pos = {\n" + " line_no = 13\n" + " line_pos = 33\n" + " }\n" + " start_pos = {\n" + " line_no = 13\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 16\n" + " line_pos = 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}"))); }
@Test public void testParser_annotations() throws IOException { copyResourceTo("/parser/tests/annotations.thrift", tmp.getRoot()); File annotations = new File(tmp.getRoot(), "annotations.thrift"); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(annotations), annotations, new TreeSet<>()); assertThat(debugString(program), is("{\n" + " program_name = \"annotations\"\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.annotations\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_enum = {\n" + " name = \"E\"\n" + " values = [\n" + " {\n" + " name = \"VAL\"\n" + " id = 0\n" + " annotations = {\n" + " \"anno\": \"str\"\n" + " \"anno.other\": \"other\"\n" + " }\n" + " start_pos = {\n" + " line_no = 4\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 4\n" + " line_pos = 45\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"e.anno\": \"E\"\n" + " }\n" + " start_pos = {\n" + " line_no = 3\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 5\n" + " line_pos = 17\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_message = {\n" + " variant = EXCEPTION\n" + " name = \"S\"\n" + " fields = [\n" + " {\n" + " id = 1\n" + " type = \"bool\"\n" + " name = \"val\"\n" + " annotations = {\n" + " \"anno\": \"str\"\n" + " }\n" + " start_pos = {\n" + " line_no = 8\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 8\n" + " line_pos = 31\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"other\": \"\"\n" + " }\n" + " start_pos = {\n" + " line_no = 7\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 10\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_service = {\n" + " name = \"Srv\"\n" + " methods = [\n" + " {\n" + " name = \"method\"\n" + " params = [\n" + " {\n" + " id = 1\n" + " type = \"i32\"\n" + " name = \"param\"\n" + " annotations = {\n" + " \"abba\": \"7\"\n" + " }\n" + " start_pos = {\n" + " line_no = 12\n" + " line_pos = 17\n" + " }\n" + " end_pos = {\n" + " line_no = 12\n" + " line_pos = 42\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"anno\": \"anno\"\n" + " }\n" + " start_pos = {\n" + " line_no = 12\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 12\n" + " line_pos = 59\n" + " }\n" + " },\n" + " {\n" + " name = \"method2\"\n" + " params = [\n" + " {\n" + " id = 1\n" + " type = \"i32\"\n" + " name = \"param\"\n" + " annotations = {\n" + " \"abba\": \"7\"\n" + " }\n" + " start_pos = {\n" + " line_no = 13\n" + " line_pos = 18\n" + " }\n" + " end_pos = {\n" + " line_no = 13\n" + " line_pos = 43\n" + " }\n" + " }\n" + " ]\n" + " exceptions = [\n" + " {\n" + " id = 1\n" + " type = \"S\"\n" + " name = \"e\"\n" + " annotations = {\n" + " \"ex\": \"667\"\n" + " }\n" + " start_pos = {\n" + " line_no = 14\n" + " line_pos = 15\n" + " }\n" + " end_pos = {\n" + " line_no = 14\n" + " line_pos = 34\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"anno\": \"anno\"\n" + " }\n" + " start_pos = {\n" + " line_no = 13\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 15\n" + " line_pos = 22\n" + " }\n" + " }\n" + " ]\n" + " annotations = {\n" + " \"bin\": \"bin\"\n" + " \"src\": \"src\"\n" + " }\n" + " start_pos = {\n" + " line_no = 11\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 16\n" + " line_pos = 29\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}")); }
@Test public void testAutoId() throws IOException { copyResourceTo("/parser/tests/autoid.thrift", tmp.getRoot()); File autoid = new File(tmp.getRoot(), "autoid.thrift"); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(autoid), autoid, new TreeSet<>()); assertEquals("{\n" + " program_name = \"autoid\"\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.autoid\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_message = {\n" + " variant = EXCEPTION\n" + " name = \"AutoId\"\n" + " fields = [\n" + " {\n" + " id = -1\n" + " type = \"string\"\n" + " name = \"message\"\n" + " start_pos = {\n" + " line_no = 4\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 4\n" + " line_pos = 19\n" + " }\n" + " },\n" + " {\n" + " id = -2\n" + " type = \"i32\"\n" + " name = \"second\"\n" + " start_pos = {\n" + " line_no = 5\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 5\n" + " line_pos = 15\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 3\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 6\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_service = {\n" + " name = \"AutoParam\"\n" + " methods = [\n" + " {\n" + " return_type = \"i32\"\n" + " name = \"method\"\n" + " params = [\n" + " {\n" + " id = -1\n" + " type = \"i32\"\n" + " name = \"a\"\n" + " start_pos = {\n" + " line_no = 9\n" + " line_pos = 16\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 21\n" + " }\n" + " },\n" + " {\n" + " id = -2\n" + " type = \"i32\"\n" + " name = \"b\"\n" + " start_pos = {\n" + " line_no = 9\n" + " line_pos = 23\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 28\n" + " }\n" + " }\n" + " ]\n" + " exceptions = [\n" + " {\n" + " id = -1\n" + " type = \"AutoId\"\n" + " name = \"auto1\"\n" + " start_pos = {\n" + " line_no = 9\n" + " line_pos = 38\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 50\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 9\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 9\n" + " line_pos = 51\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 8\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 10\n" + " line_pos = 1\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}", debugString(program)); }
@Test public void testAutoValue() throws IOException { copyResourceTo("/parser/tests/autovalue.thrift", tmp.getRoot()); File autovalue = new File(tmp.getRoot(), "autovalue.thrift"); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(autovalue), autovalue, new TreeSet<>()); assertEquals("{\n" + " program_name = \"autovalue\"\n" + " namespaces = {\n" + " \"java\": \"net.morimekta.test.autoid\"\n" + " }\n" + " decl = [\n" + " {\n" + " decl_enum = {\n" + " name = \"AutoValue\"\n" + " values = [\n" + " {\n" + " name = \"FIRST\"\n" + " id = 0\n" + " start_pos = {\n" + " line_no = 4\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 4\n" + " line_pos = 10\n" + " }\n" + " },\n" + " {\n" + " name = \"SECOND\"\n" + " id = 1\n" + " start_pos = {\n" + " line_no = 5\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 5\n" + " line_pos = 11\n" + " }\n" + " },\n" + " {\n" + " name = \"THIRD\"\n" + " id = 2\n" + " start_pos = {\n" + " line_no = 6\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 6\n" + " line_pos = 10\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 3\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 7\n" + " line_pos = 2\n" + " }\n" + " }\n" + " },\n" + " {\n" + " decl_enum = {\n" + " name = \"PartialAutoValue\"\n" + " values = [\n" + " {\n" + " name = \"FIRST\"\n" + " id = 5\n" + " start_pos = {\n" + " line_no = 10\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 10\n" + " line_pos = 14\n" + " }\n" + " },\n" + " {\n" + " name = \"SECOND\"\n" + " id = 6\n" + " start_pos = {\n" + " line_no = 11\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 11\n" + " line_pos = 11\n" + " }\n" + " },\n" + " {\n" + " name = \"THIRD\"\n" + " id = 7\n" + " start_pos = {\n" + " line_no = 12\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 12\n" + " line_pos = 10\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 9\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 13\n" + " line_pos = 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}", debugString(program)); }
@Test public void testRegression() throws IOException { File regressed = copyResourceTo("/idl/backend-libraries/common_fileserver.thrift", tmp.getRoot()); ThriftProgramParser parser = new ThriftProgramParser(); ProgramType program = parser.parse(new FileInputStream(regressed), regressed, new TreeSet<>()); assertEquals("{\n" + " program_name = \"common_fileserver\"\n" + " decl = [\n" + " {\n" + " decl_message = {\n" + " name = \"FileServerConfig\"\n" + " fields = [\n" + " {\n" + " documentation = \"Configuration for the /download… URL.\\nSee https: " id = 3\n" + " requirement = OPTIONAL\n" + " type = \"i32\"\n" + " name = \"download_url\"\n" + " start_pos = {\n" + " line_no = 6\n" + " line_pos = 5\n" + " }\n" + " end_pos = {\n" + " line_no = 6\n" + " line_pos = 33\n" + " }\n" + " }\n" + " ]\n" + " start_pos = {\n" + " line_no = 1\n" + " line_pos = 1\n" + " }\n" + " end_pos = {\n" + " line_no = 7\n" + " line_pos = 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}", debugString(program)); }