summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2016-04-29 23:10:23 +0200
committerPeter Wu <peter@lekensteyn.nl>2016-04-29 23:10:23 +0200
commit58ee6d89a6191d6ccae0d4394ca10a7f9c4deebd (patch)
treed29add20ee781016ef3b253a97a76a7e3fd7a6d6
parentcc66b84299cb8ac42beda43718c4ceccbf16f41c (diff)
downloadRegexTest-58ee6d89a6191d6ccae0d4394ca10a7f9c4deebd.tar.gz
Implement Matlab comments
-rw-r--r--src/regex/RegexTest.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/regex/RegexTest.java b/src/regex/RegexTest.java
index 56d49bb..fff0796 100644
--- a/src/regex/RegexTest.java
+++ b/src/regex/RegexTest.java
@@ -125,11 +125,69 @@ public class RegexTest {
System.out.println();
}
+ static void checkMatlabComment() {
+ /* EOL ::= #0a (* LF character *)
+ * WSP ::= " " | #09 (* horizontal space and tab *)
+ * CharWithoutEOL ::= Char - EOL
+ * SingleLineComment ::= "%" CharWithoutEOL*
+ * MultiLineComment ::= "%{" WSP* EOL (CharWithoutEOL* EOL)* WSP* "%}"
+ * Comment ::= SingleLineComment | MultiLineComment
+ *
+ * According to the Mathlab docs[1], there can be no other data on the
+ * same line. According to a Wikibooks article[2] and by manual testing
+ * in Mathlab (actually Octave), it turns out that whitespace is allowed
+ * on the same line as "%{" and "%}". We only care about the begin/end
+ * of comment, so excluding any leading and trailing whitespace.
+ *
+ * The EOL is not well-specified, assume LF as that is the format that
+ * seems to be used[3].
+ *
+ * [1]: http://nl.mathworks.com/help/matlab/matlab_prog/comments.html
+ * [2]: https://en.wikibooks.org/wiki/MATLAB_Programming/Comments
+ * [3]: https://stackoverflow.com/q/6823168
+ */
+ String sl = "%[^\n]*";
+ String ml = "%\\{[ \t]*\n" + "([^\n]*\n)*" + "[ \t]*%\\}";
+ RegexTest reMatlabComment = new RegexTest("(" + sl + ")|(" + ml + ")");
+
+ // single line comments
+ reMatlabComment.assertOk("%");
+ reMatlabComment.assertOk("%abc");
+ reMatlabComment.assertOk("% abc");
+ reMatlabComment.assertOk("%%");
+ reMatlabComment.assertOk("%% abc");
+ reMatlabComment.assertFail("%\n");
+ reMatlabComment.assertFail("%a\n");
+ reMatlabComment.assertFail("%%\n");
+ reMatlabComment.assertFail("%a\n%b");
+
+ // Single-line comments that could be mistaken for multi-line comments
+ reMatlabComment.assertOk("%{%}");
+ reMatlabComment.assertOk("%{%}x");
+ reMatlabComment.assertOk("%{x%}");
+ reMatlabComment.assertOk("%{%}%}");
+
+ // Multi-line comments
+ reMatlabComment.assertOk("%{\n%}");
+ reMatlabComment.assertOk("%{\n\n%}");
+ reMatlabComment.assertOk("%{\nxxx\n%}");
+ reMatlabComment.assertOk("%{\n\nxxx\n%}");
+ reMatlabComment.assertOk("%{\nxxx\n\n%}");
+ reMatlabComment.assertOk("%{ \n %}");
+ reMatlabComment.assertOk("%{ \nx\n \t %}");
+ reMatlabComment.assertFail("%{x\n%}");
+ reMatlabComment.assertFail("%{\nx%}");
+ reMatlabComment.assertFail("%{\n%}x");
+ reMatlabComment.assertFail("%{ \nx %}");
+ reMatlabComment.assertFail("%{\n%}%}");
+ }
+
public static void main(String[] args) {
checkId();
checkNAT();
checkFLOAT();
checkString();
+ checkMatlabComment();
System.out.println("Passed.");
}
}