我为 ParseKit 编写了一个 C 语法,它工作得很好,但让我发疯的是预处理器语句。预处理器语句的正确符号定义是什么?
这是我尝试过的简短示例......
@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';
@commentState.fallbackState = delimitState;
@delimitState.fallbackState = symbolState;
@start = Empty | comments | preprocessor;
comments = comment*;
comment = Comment;
@symbols = '#include';
preprocessor = preprocessorIncludes;
preprocessorIncludes = preprocessorIncludeStatement*;
preprocessorIncludeStatement = preprocessorInclude quotedFileName*;
preprocessorInclude = '#include';
quotedFileName = QuotedString;
...但是它不起作用。将其作为简化的语法示例来捕获注释并包含带引号的语句(而不是 < >)。我在这个简单的文件上尝试了这个语法......
/*
* Cryptographic API.
*
* RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
*
* Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
*
* Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
*
* 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 2 of the License, or (at your option)
* any later version.
*
*/
// Here's one line comment
/* One line multiline comment */
#include "ripemd.h"
/* 2nd one line multiline comment */
... 并以/* 一行多行注释 */结束,将其报告为注释标记,然后默默地失败。
所以我尝试将“#include”符号分隔为...
@symbolState = '#' '#';
@symbol = '#';
numSymbol = '#';
preprocessorInclude = numSymbol 'include';
...但仍然无济于事。
也许 Todd 可以提供帮助,但处理“#include”等“符号”的正确方法是什么?
最佳答案
ParseKit 的开发者|在这里。
罗伯特,你的语法非常接近,但我发现你使用嵌套的 *
(零个或多个)修饰符导致语法失败。
我认为问题在于您的 @start
语法生成已经将 Empty
作为顶级选项(|
ed 与其他两个产品),但是 comments
和 preprocessor
的子产品都包含带有 *
(零或多个)修饰符的产品。那些 *
实际上应该是 +
(一个或多个)修饰符,因为您已经考虑了顶级 Empty
的零情况>.
我不完全确定,但我认为这不是 ParseKit 独有的问题,而是我怀疑语法有问题,并且任何此类语法工具包都可能出现过此问题。 (可能是错的)
考虑到这一点,我对语法进行了一些小的调整。这是经过小调整的编辑语法:
@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';
@commentState.fallbackState = delimitState;
@delimitState.fallbackState = symbolState;
@start = (comments | preprocessor)*;
comments = comment+;
comment = Comment;
@symbols = '#include';
preprocessor = preprocessorIncludes;
preprocessorIncludes = preprocessorIncludeStatement+;
preprocessorIncludeStatement = preprocessorInclude quotedFileName;
preprocessorInclude = '#include';
quotedFileName = QuotedString;
请注意我用 *
替换了顶层的 Empty
。我用 +
交换了嵌套的 *
。
使用这个编辑过的语法,我得到了想要的输出(为清楚起见略微截断):
[/*
* Cryptographic API.
...
*/, // Here's one line comment, /* One line multiline comment */, #include, "ripemd.h", /* 2nd one line multiline comment */]/*
* Cryptographic API.
...
*//// Here's one line comment//* One line multiline comment *//#include/"ripemd.h"//* 2nd one line multiline comment */^
此外,为了找到问题,我重写了语法以使其更简单。这样更容易找到问题。然后我将我发现的重新应用到你原来的语法中。如果您有兴趣,这是我想出的简化语法。这就是我在脑海中对这个特定语法的看法:
@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';
@start = (comment | macro)*;
comment = Comment;
macro = include; // to support other macros, add: ` | define | ifdef` etc.
include = '#' 'include' QuotedString;
关于objective-c - ParseKit - 如何正确处理预处理器语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502766/