博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Predictive Parsing的ABNF语法分析器(五)——AbnfParser文法解析器之单字符的情形(如HTAB、LF、CR、SP)...
阅读量:7227 次
发布时间:2019-06-29

本文共 5452 字,大约阅读时间需要 18 分钟。

先来看看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 
. */// 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 
. */ // HTAB = %x09 @Test public void testHTAB() throws Exception { Tester
tester = new Tester
() { @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
tester = new Tester
() { @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
tester = new Tester
() { @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
tester = new Tester
() { @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); }

转载地址:http://igbfm.baihongyu.com/

你可能感兴趣的文章
JMM-java内存模型
查看>>
iOS的soap应用(webservice) 开发
查看>>
Delphi listview 点击列头排序
查看>>
android preference page
查看>>
mysql索引挑选
查看>>
关于冰岛足球的段子
查看>>
在 Windows 中安装 Laravel 5.1.X
查看>>
TeamViewer 9发布-在Linux下安装运行
查看>>
Centos7 Gitea安装教程 - 一款易搭建,运行快的Git服务器
查看>>
CentOS minimal 网络配置
查看>>
Nginx架构
查看>>
为什么结构体中的数组不能用const int变量指定大小?
查看>>
模板特化疑问
查看>>
ruby多线程理解
查看>>
享用甜点时要不要来杯咖啡?Java 与树莓派 (Raspberry PI)
查看>>
精通Oracle 10g Pl/SQL编程
查看>>
邱怡轩:R中大规模矩阵的SVD与矩阵补全
查看>>
C++ Primer习题集(第5版)
查看>>
centos7 mysql 5.7 yum安装
查看>>
JSOUP简单应用
查看>>