基于Predictive Parsing的ABNF语法分析器(五)——AbnfParser文法解析器之单字符的情形(如HTAB、LF、CR、SP)
先来看看AbnfParser类如何对ABNF文法中最简单的一些单字节符号如何进行解析,这些单字节符号包括跳格、换行、回车和空格:
/* This file is one of the component a Context-free Grammar Parser Generator, which accept a piece of text as the input, and generates a parser for the inputted context-free grammar. Copyright (C) 2013, Junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ // HTAB = %x09 protected String HTAB() throws IOException, MatchException { // 断言下一个字符为0x09(否则抛出MatchException异常) assertMatch(is.peek(), 0x09); int value = is.read(); // 返回HTAB的字符串值 return String.valueOf((char)value); } // LF = %x0A protected String LF() throws IOException, MatchException { // 断言下一个字符为0x0A(否则抛出MatchException异常) assertMatch(is.peek(), 0x0A); int value = is.read(); // 返回换行的字符串值 return String.valueOf((char)value); } // CR = %x0D protected String CR() throws IOException, MatchException { // 断言下一个字符为0x0D(否则抛出MatchException异常) assertMatch(is.peek(), 0x0D); int value = is.read(); // 返回回车的字符串值 return String.valueOf((char)value); } // SP = %x20 protected String SP() throws IOException, MatchException { // 断言下一个字符为0x20(否则抛出MatchException异常) assertMatch(is.peek(), 0x20); int value = is.read(); // 返回空格的字符串值 return String.valueOf((char)value); }
这些单字符的解析函数不需要向前看(预测),因为当他们一旦被调用,就意味着必须是相应的字符,否则就是抛出异常了。
接下来看看上面各个函数对应的单元测试函数。
/* This file is one of the component a Context-free Grammar Parser Generator, which accept a piece of text as the input, and generates a parser for the inputted context-free grammar. Copyright (C) 2013, Junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ // HTAB = %x09 @Test public void testHTAB() throws Exception { Tester<String> tester = new Tester<String>() { @Override public String test(AbnfParser parser) throws MatchException, IOException { // 测试器负责调用被测函数AbnfParser.HTAB() return parser.HTAB(); } }; // 测试用例1:一个0x09字符输入的情形 Assert.assertEquals(String.valueOf((char)0x09), AbnfParserFactory.newInstance(new char[] {0x09}).HTAB()); // 测试用例2:两个0x09字符输入的情形 Assert.assertEquals(String.valueOf((char) 0x09), AbnfParserFactory.newInstance(new char[]{0x09, 0x09}).HTAB()); // 测试用例3:一个0x0D字符输入的情形(异常) Assertion.assertMatchException("" + (char) 0x0D, tester, 1, 1); // 测试用例3:空字符输入的情形(异常) Assertion.assertMatchException("", tester, 1, 1); } // LF = %x0A @Test public void testLF() throws Exception { Tester<String> tester = new Tester<String>() { @Override public String test(AbnfParser parser) throws MatchException, IOException { // 测试器负责调用被测函数AbnfParser.LF() return parser.LF(); } }; // 测试用例1:一个0x0A字符输入的情形 Assert.assertEquals(String.valueOf((char)0x0A), AbnfParserFactory.newInstance(new char[] {0x0A}).LF()); // 测试用例2:两个0x0A字符输入的情形 Assert.assertEquals(String.valueOf((char) 0x0A), AbnfParserFactory.newInstance(new char[]{0x0A, 0x0A}).LF()); // 测试用例3:一个0x0D字符输入的情形(异常) Assertion.assertMatchException("", tester, 1, 1); // 测试用例3:空字符输入的情形(异常) Assertion.assertMatchException("" + (char)0x0D, tester, 1, 1); } // CR = %x0D @Test public void testCR() throws Exception { Tester<String> tester = new Tester<String>() { @Override public String test(AbnfParser parser) throws MatchException, IOException { return parser.CR(); } }; Assert.assertEquals(String.valueOf((char) 0x0D), AbnfParserFactory.newInstance(new char[]{0x0D, 0x0D}).CR()); Assertion.assertMatchException("", tester, 1, 1); Assertion.assertMatchException("" + (char)0x0A, tester, 1, 1); } // SP = %x20 @Test public void testSP() throws Exception { Tester<String> tester = new Tester<String>() { @Override public String test(AbnfParser parser) throws MatchException, IOException { return parser.SP(); } }; Assert.assertEquals(String.valueOf((char)0x20), AbnfParserFactory.newInstance(new char[] {0x20, 0x20}).SP()); Assertion.assertMatchException("", tester, 1, 1); Assertion.assertMatchException("" + (char)0x0D, tester, 1, 1); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2012-06-08 Java内存结构学习总结