Thread: [Japi-cvs] SF.net SVN: japi: [373] tools/cstyle/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-06-08 21:21:14
|
Revision: 373 http://svn.sourceforge.net/japi/?rev=373&view=rev Author: christianhujer Date: 2007-06-08 14:21:12 -0700 (Fri, 08 Jun 2007) Log Message: ----------- Added code with command and physical parser. Added Paths: ----------- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java Added: tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java =================================================================== --- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java (rev 0) +++ tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java 2007-06-08 21:21:12 UTC (rev 373) @@ -0,0 +1,195 @@ +/* + * CStyle is a program that verifies that a C source code adheres to a specific coding convention. + * Copyright (C) 2007 Christian Hujer + * + * 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. + * + * 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.japi.cstyle; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; +import java.util.List; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; +import org.jetbrains.annotations.NotNull; + +/** CStyle is a simple program that checks whether a C source or header file adhers to a specific code convention. + * This is work under progress, so don't expect too much from it yet. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class CStyle extends BasicCommand { + + /** Main program. + * @param args Command line arguments. + */ + public static void main(final String... args) { + ArgParser.simpleParseAndRun(new CStyle(), args); + } + + /** The encoding to use when reading file. + * Defaults to the system's default encoding. + */ + @NotNull private String encoding = Charset.defaultCharset().name(); + + /** The current physicalMode of the parser. */ + @NotNull private PhysicalMode physicalMode = PhysicalMode.NORMAL; + + /** {@inheritDoc} */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + public int run(@NotNull final List<String> args) throws Exception { + if (args.size() == 0) { + check(System.in); + } + return 0; + } + + /** Checks a single file. + * @param filename Name of the file to read from. + * @throws IOException In case of I/O problems. + */ + public void check(final String filename) throws IOException { + check(new File(filename)); + } + + /** Checks a single file. + * @param file File to read from. + * @throws IOException In case of I/O problems. + */ + public void check(final File file) throws IOException { + final InputStream in = new FileInputStream(file); + try { + check(in); + } finally { + in.close(); + } + } + + /** Checks a single stream. + * @param stream Stream to read from. + * @throws IOException In case of I/O problems. + */ + public void check(final InputStream stream) throws IOException { + final BufferedReader in = new BufferedReader(new InputStreamReader(stream, encoding)); + check(in); + } + + /** Checks a single reader. + * @param in Reader to read from. + * @throws IOException In case of I/O problems. + */ + public void check(final Reader in) throws IOException { + physicalMode = PhysicalMode.NORMAL; + for (int c; (c = in.read()) != -1;) { + parse((char) c); + } + } + + /** Reset the parser to its initial state. */ + public void reset() { + physicalMode = PhysicalMode.NORMAL; + } + + /** Processes a single character in the parser. + * @param c Character to parse + */ + public void parse(final char c) { + switch (physicalMode) { + case NORMAL: + switch (c) { + case '\\': physicalMode = PhysicalMode.ESCAPE; break; + case '/': physicalMode = PhysicalMode.SLASH; break; + case '"': physicalMode = PhysicalMode.STRING; break; + case '\'': physicalMode = PhysicalMode.CHAR; break; + } + break; + case ESCAPE: + physicalMode = PhysicalMode.NORMAL; + break; + case SLASH: + switch (c) { + case '/': physicalMode = PhysicalMode.EOL_COMMENT; break; + case '*': physicalMode = PhysicalMode.ML_COMMENT; break; + default: physicalMode = PhysicalMode.NORMAL; break; + } + break; + case EOL_COMMENT: + switch (c) { + case '\n': physicalMode = PhysicalMode.NORMAL; break; + } + break; + case ML_COMMENT: + switch (c) { + case '*': physicalMode = PhysicalMode.ML_COMMENT_STAR; break; + } + break; + case ML_COMMENT_STAR: + switch (c) { + case '/': physicalMode = PhysicalMode.NORMAL; break; + case '*': physicalMode = PhysicalMode.ML_COMMENT_STAR; break; + default: physicalMode = PhysicalMode.ML_COMMENT; break; + } + break; + case STRING: + switch (c) { + case '"': physicalMode = PhysicalMode.NORMAL; break; + case '\\': physicalMode = PhysicalMode.STRING_ESCAPE; break; + } + break; + case STRING_ESCAPE: + physicalMode = PhysicalMode.STRING; + break; + case CHAR: + switch (c) { + case '\'': physicalMode = PhysicalMode.NORMAL; break; + case '\\': physicalMode = PhysicalMode.CHAR_ESCAPE; break; + } + break; + case CHAR_ESCAPE: + physicalMode = PhysicalMode.CHAR; + break; + default: + assert false; + } + } + + /** Returns the current physicalMode of the parser. + * @return The current physicalMode of the parser. + */ + @NotNull public PhysicalMode getPhysicalMode() { + return physicalMode; + } + + /** The physicalMode of the physical parser. */ + public enum PhysicalMode { + NORMAL, + ESCAPE, + SLASH, + EOL_COMMENT, + ML_COMMENT, + ML_COMMENT_STAR, + STRING, + STRING_ESCAPE, + CHAR, + CHAR_ESCAPE + } // enum PhysicalMode + +} // class CStyle Property changes on: tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java =================================================================== --- tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java (rev 0) +++ tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java 2007-06-08 21:21:12 UTC (rev 373) @@ -0,0 +1,51 @@ +/* + * CStyle is a program that verifies that a C source code adheres to a specific coding convention. + * Copyright (C) 2007 Christian Hujer + * + * 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. + * + * 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package test.net.sf.japi.cstyle; + +import net.sf.japi.cstyle.CStyle; +import org.junit.Test; +import org.junit.Assert; + +/** Unit test for {@link CStyle}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class CStyleTest { + + /** Tests whether {@link CStyle#parse(char)} works. */ + @Test public void testParse() { + final CStyle testling = new CStyle(); + final String testString = "if (foo > 'x') { // check x\n printf(\"%d\");\n}"; + final char[] chars = testString.toCharArray(); + for (final char c : chars) { + testling.parse(c); + } + Assert.assertEquals("mode now must be NORMAL.", CStyle.PhysicalMode.NORMAL, testling.getPhysicalMode()); + } + + /** Tests whether {@link CStyle#reset()} works. */ + @Test public void testReset() { + final CStyle testling = new CStyle(); + testling.parse('\''); + Assert.assertEquals("mode now must be CHAR.", CStyle.PhysicalMode.CHAR, testling.getPhysicalMode()); + testling.reset(); + Assert.assertEquals("After reset, mode must be NORMAL", CStyle.PhysicalMode.NORMAL, testling.getPhysicalMode()); + } + +} // class CStyleTest Property changes on: tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <chr...@us...> - 2007-06-09 13:27:34
|
Revision: 374 http://svn.sourceforge.net/japi/?rev=374&view=rev Author: christianhujer Date: 2007-06-09 06:27:33 -0700 (Sat, 09 Jun 2007) Log Message: ----------- Added encoding property and memory for previous physical mode. Modified Paths: -------------- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java Added Paths: ----------- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.properties Modified: tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java =================================================================== --- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java 2007-06-08 21:21:12 UTC (rev 373) +++ tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.java 2007-06-09 13:27:33 UTC (rev 374) @@ -30,6 +30,7 @@ import java.util.List; import net.sf.japi.io.args.ArgParser; import net.sf.japi.io.args.BasicCommand; +import net.sf.japi.io.args.Option; import org.jetbrains.annotations.NotNull; /** CStyle is a simple program that checks whether a C source or header file adhers to a specific code convention. @@ -50,9 +51,28 @@ */ @NotNull private String encoding = Charset.defaultCharset().name(); + /** The previous physicalMode of the parser. */ + @NotNull private PhysicalMode previousPhysicalMode = PhysicalMode.NORMAL; + /** The current physicalMode of the parser. */ @NotNull private PhysicalMode physicalMode = PhysicalMode.NORMAL; + /** The current token of the parser. */ + @NotNull private final StringBuilder currentToken = new StringBuilder(); + + /** Creates a CStyle command. */ + public CStyle() { + reset(); + } + + /** Sets the encoding to use when reading source files. + * @param encoding Encoding to use + */ + @Option({"e", "encoding"}) + public void setEncoding(@NotNull final String encoding) { + this.encoding = Charset.forName(encoding).name(); + } + /** {@inheritDoc} */ @SuppressWarnings({"InstanceMethodNamingConvention"}) public int run(@NotNull final List<String> args) throws Exception { @@ -106,13 +126,17 @@ /** Reset the parser to its initial state. */ public void reset() { physicalMode = PhysicalMode.NORMAL; + previousPhysicalMode = PhysicalMode.NORMAL; + currentToken.setLength(0); } /** Processes a single character in the parser. * @param c Character to parse */ public void parse(final char c) { - switch (physicalMode) { + previousPhysicalMode = physicalMode; + // Step 1: Parse the character and determine the new mode. + switch (previousPhysicalMode) { case NORMAL: switch (c) { case '\\': physicalMode = PhysicalMode.ESCAPE; break; @@ -169,8 +193,19 @@ default: assert false; } + // Step 2: Tokenize + switch (physicalMode) { + case NORMAL: + } } + /** Returns the previous physicalMode of the parser. + * @return The previous physicalMode of the parser. + */ + @NotNull public PhysicalMode getPreviousPhysicalMode() { + return previousPhysicalMode; + } + /** Returns the current physicalMode of the parser. * @return The current physicalMode of the parser. */ Added: tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.properties =================================================================== --- tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.properties (rev 0) +++ tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.properties 2007-06-09 13:27:33 UTC (rev 374) @@ -0,0 +1,20 @@ +# +# CStyle is a program that verifies that a C source code adheres to a specific coding convention. +# Copyright (C) 2007 Christian Hujer +# +# 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. +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +setEncoding=Specify the encoding to use when reading the sources. Defaults to the underlying platform's default encoding. \ No newline at end of file Property changes on: tools/cstyle/trunk/src/net/sf/japi/cstyle/CStyle.properties ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java =================================================================== --- tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java 2007-06-08 21:21:12 UTC (rev 373) +++ tools/cstyle/trunk/src/test/net/sf/japi/cstyle/CStyleTest.java 2007-06-09 13:27:33 UTC (rev 374) @@ -43,9 +43,11 @@ @Test public void testReset() { final CStyle testling = new CStyle(); testling.parse('\''); + Assert.assertEquals("previous mode must stay NORMAL.", CStyle.PhysicalMode.NORMAL, testling.getPreviousPhysicalMode()); Assert.assertEquals("mode now must be CHAR.", CStyle.PhysicalMode.CHAR, testling.getPhysicalMode()); testling.reset(); Assert.assertEquals("After reset, mode must be NORMAL", CStyle.PhysicalMode.NORMAL, testling.getPhysicalMode()); + Assert.assertEquals("After reset, previous mode must be NORMAL", CStyle.PhysicalMode.NORMAL, testling.getPreviousPhysicalMode()); } } // class CStyleTest This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |