From 58ee6d89a6191d6ccae0d4394ca10a7f9c4deebd Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 29 Apr 2016 23:10:23 +0200 Subject: Implement Matlab comments --- src/regex/RegexTest.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) 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."); } } -- cgit v1.2.1