Skip to content

Commit

Permalink
Block lines bracketed and : removed
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcn committed Jan 14, 2013
1 parent 222c7c9 commit bd570f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
16 changes: 8 additions & 8 deletions PythonEmbrace.Tests/SimpleTests.cs
Expand Up @@ -28,7 +28,7 @@ public void SingleIf()
if c != u';':
self.stream.unget(c)
----
if c != u';':
if( c != u';')
{
self.stream.unget(c);
}
Expand All @@ -46,7 +46,7 @@ public void BracesBeforeBlankLines()
self.stream.unget(c)
----
if c != u';':
if( c != u';')
{
self.stream.unget(c);
}
Expand All @@ -65,7 +65,7 @@ public void BlankLinesDoNotAffectIndentationLevel()
self.stream.unget(c)
----
if c != u';':
if( c != u';')
{
self.stream.unget(c);
Expand All @@ -85,7 +85,7 @@ public void BracketsAreImplicitLineContinuation()
self.stream.unget(c)
----
if (c != u';' and
c != ','):
c != ',')
{
self.stream.unget(c);
}
Expand All @@ -103,7 +103,7 @@ public void BracketsInStringsAreIgnored()
self.stream.unget(c)
----
if (c != ')' and
c != ','):
c != ',')
{
self.stream.unget(c);
}
Expand All @@ -121,7 +121,7 @@ public void EscapedQuotesDontEndStringsIgnored()
self.stream.unget(c)
----
if (c != ')\'' and
c != ','):
c != ',')
{
self.stream.unget(c);
}
Expand All @@ -138,8 +138,8 @@ public void ExplicitLineContinuation()
u';':
self.stream.unget(c)
----
if c != \
u';':
if( c != \
u';')
{
self.stream.unget(c);
}
Expand Down
52 changes: 48 additions & 4 deletions PythonEmbrace/LogicalLine.cs
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace PythonEmbrace
{
Expand All @@ -16,19 +17,62 @@ public string Text
{
if (_lastSignificantCharIndex != -1)
{
string significantText = _text.ToString(0, _lastSignificantCharIndex + 1);
string insignificantText = _text.ToString(significantText.Length, _text.Length - significantText.Length);

if (_text[_lastSignificantCharIndex] == ':')
{
// Block start
// FIXME remove colon and bracket if necessary
return ConvertBlockLine(significantText) + insignificantText;
}
else
{
// End with a semicolon
_text.Insert(_lastSignificantCharIndex + 1, ';');
// Add a semicolon
return significantText + ";" + insignificantText;
}
}
else
{
// Entirely whitespace, or just a comment
return _text.ToString();
}
}
}

private static readonly string[] BracketedKeywords = { "if", "elif", "for", "while", "except" };

private static readonly Regex BlockLinePattern =
new Regex(@"^(\s*)(if|else|elif|def|for|while|try|except|class)(.*):$",
RegexOptions.Compiled | RegexOptions.Singleline);

private string ConvertBlockLine(string significantText)
{
if (BlockLinePattern.IsMatch(significantText))
{
// FIXME remove colon and brackets if necessary
Match match = BlockLinePattern.Match(significantText);

return _text.ToString();
string whitespace = match.Groups[1].Value;
string keyword = match.Groups[2].Value;
string remainder = match.Groups[3].Value;

bool needsBrackets = BracketedKeywords.Contains(keyword) && !remainder.TrimStart().StartsWith("(");

if (keyword == "elif")
{
keyword = "else if";
}
else if (keyword == "except")
{
keyword = "catch";
}

return whitespace + keyword + (needsBrackets ? "(" : "") + remainder + (needsBrackets ? ")" : "");

}
else
{
throw new Exception("Line " + _lineNumber + " ends with a : but doesn't match BlockLinePattern");
}
}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -6,6 +6,10 @@ A tool to help with the manual conversion of Python code to C family languages (
It aims to take some of the grunt work out of the manual conversion. Run it on a *valid* python input file to produce an output file with:
* Braces `{}` added, based on Python's indentation rules
* Semicolons `;` added to the end of statements
* Colons `:` removed from the end of lines that begin blocks
* Brackets added to unbracked `if`, `elif`, `for`, `while`, and `except` statements
* `except` changed into `catch`
* `elif` changed into `else if`

Usage: `PythonEmbrace.exe file.py`

Expand Down

0 comments on commit bd570f3

Please sign in to comment.