Permalink
Browse files

Fixed all issues.

  • Loading branch information...
1 parent 4bba379 commit 414b48a04833c6de4c78f5df9513c96fca9aceaa @chrisdunelm committed Oct 11, 2011
View
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoffeeParserTest", "CoffeeP
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoffeeLite", "CoffeeLite\CoffeeLite.csproj", "{B03E0173-346A-43B8-8E36-3630DF9C904B}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parser", "Parser\Parser.csproj", "{6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -47,6 +49,16 @@ Global
{B03E0173-346A-43B8-8E36-3630DF9C904B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{B03E0173-346A-43B8-8E36-3630DF9C904B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B03E0173-346A-43B8-8E36-3630DF9C904B}.Release|x86.ActiveCfg = Release|Any CPU
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Debug|Any CPU.ActiveCfg = Debug|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Debug|Mixed Platforms.Build.0 = Debug|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Debug|x86.ActiveCfg = Debug|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Debug|x86.Build.0 = Debug|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Release|Any CPU.ActiveCfg = Release|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Release|Mixed Platforms.ActiveCfg = Release|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Release|Mixed Platforms.Build.0 = Release|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Release|x86.ActiveCfg = Release|x86
+ {6B5B29BF-DC7E-4A9C-A660-C75F5867BBC1}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -38,5 +38,10 @@ static class ClassificationTypes {
[BaseDefinition(VisualFormatNames.Coffee)]
internal static ClassificationTypeDefinition CommentClassificationDefinition = null;
- }
+ [Export]
+ [Name(VisualFormatNames.CoffeeThis)]
+ [BaseDefinition(VisualFormatNames.Coffee)]
+ internal static ClassificationTypeDefinition ThisClassificationDefinition = null;
+
+ }
}
View
@@ -17,6 +17,7 @@ internal class Classifier : IClassifier {
this.clsCoffeeKeyword = classificationRegistry.GetClassificationType(VisualFormatNames.CoffeeKeyword);
this.clsCoffeeNumericLiteral = classificationRegistry.GetClassificationType(VisualFormatNames.CoffeeNumericLiteral);
this.clsCoffeeComment = classificationRegistry.GetClassificationType(VisualFormatNames.CoffeeComment);
+ this.clsThis = classificationRegistry.GetClassificationType(VisualFormatNames.CoffeeThis);
overview.MultiLinesChanged += (o, e) => {
if (this.latestSnapshot != null && this.ClassificationChanged != null) {
var spans = new NormalizedSnapshotSpanCollection(
@@ -36,6 +37,7 @@ internal class Classifier : IClassifier {
private IClassificationType clsCoffeeKeyword;
private IClassificationType clsCoffeeNumericLiteral;
private IClassificationType clsCoffeeComment;
+ private IClassificationType clsThis;
private ITextSnapshot latestSnapshot = null;
public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
@@ -107,6 +109,8 @@ internal class Classifier : IClassifier {
return this.clsCoffeeString;
case Token.Comment:
return this.clsCoffeeComment;
+ case Token.This:
+ return this.clsThis;
default:
return null;
}
@@ -57,7 +57,7 @@ public class MultiLinesChangedEventArgs : EventArgs {
private static MlInfo[] multiLinesDelimiters =
{
Tuple.Create("###", "###", MultiLineType.Comment, (string)null),
- Tuple.Create("/*", "*/", MultiLineType.Comment, (string)null),
+ //Tuple.Create("/*", "*/", MultiLineType.Comment, (string)null),
Tuple.Create("`", "`", MultiLineType.Backtick, (string)null),
Tuple.Create("'", "'", MultiLineType.StringSingle, "\\'"),
Tuple.Create("\"", "\"", MultiLineType.StringDouble, "\\\""),
@@ -90,16 +90,16 @@ public class MultiLinesChangedEventArgs : EventArgs {
var text = ss.GetText();
MlInfo curMultiline = null;
int startPos = -1;
- bool inComment = false;
+ bool inSingleLineComment = false;
for (int i = 0, n = text.Length; i < n; i++) {
- if (inComment) {
+ if (inSingleLineComment) {
if (text[i].In('\r', '\n')) {
- inComment = false;
+ inSingleLineComment = false;
}
} else {
if (curMultiline == null) {
- if (text[i] == '#') {
- inComment = true;
+ if (text[i] == '#' && !text.IsAt(i, "###") && !text.IsAt(i - 1, "###") && !text.IsAt(i - 1, "###")) {
+ inSingleLineComment = true;
} else {
curMultiline = multiLinesDelimiters.FirstOrDefault(x => text.IsAt(i, x.Item1));
if (curMultiline != null) {
View
@@ -6,8 +6,17 @@
namespace CoffeeSyntax {
static class Extensions {
- public static bool IsAt(this string s, int ofs, string isAt) {
- return s.Skip(ofs).Take(isAt.Length).SequenceEqual(isAt);
+ public static bool IsAt(this string s, int ofs, string isAtOfs) {
+ int n = s.Length;
+ for (int i = 0; i < isAtOfs.Length; i++, ofs++) {
+ if (ofs < 0 || ofs >= n) {
+ return false;
+ }
+ if (s[ofs] != isAtOfs[i]) {
+ return false;
+ }
+ }
+ return true;
}
public static TResult NullThru<T, TResult>(this T o, Func<T, TResult> fn, TResult @default = default(TResult)) {
@@ -12,6 +12,7 @@ static class VisualFormatNames {
public const string CoffeeKeyword = "coffee.keyword";
public const string CoffeeNumericLiteral = "coffee.numericliteral";
public const string CoffeeComment = "coffee.comment";
+ public const string CoffeeThis = "coffee.this";
}
}
@@ -70,5 +70,17 @@ sealed class CommentFormat : ClassificationFormatDefinition {
}
}
- }
+ [Export(typeof(EditorFormatDefinition))]
+ [ClassificationType(ClassificationTypeNames = VisualFormatNames.CoffeeThis)]
+ [Name(VisualFormatNames.CoffeeThis)]
+ [UserVisible(true)]
+ [DisplayName("Coffee this")]
+ [Order(Before = Priority.Default)]
+ sealed class CommentThis : ClassificationFormatDefinition {
+ public CommentThis() {
+ this.ForegroundColor = Colors.Orange;
+ }
+ }
+
+ }
}
@@ -13,7 +13,6 @@
<Edition>Ultimate</Edition>
<Edition>Premium</Edition>
<Edition>Pro</Edition>
- <Edition>Express_All</Edition>
<Edition>IntegratedShell</Edition>
</VisualStudio>
</SupportedProducts>
@@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Extensions.cs" />
<Compile Include="Parser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Token.cs" />
View
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CoffeeParser {
+ static class Extensions {
+
+ public static bool In<T>(this T item, params T[] isIn) {
+ return isIn.Contains(item);
+ }
+
+ public static IEnumerable<T> Concat<T>(this IEnumerable<T> a, T b) {
+ return a.Concat(new[] { b });
+ }
+
+ }
+}
View
@@ -5,47 +5,62 @@
namespace CoffeeParser {
- using ParseResult = Tuple<int, Token>;
+ using ParseResult = Tuple<int, Token, string>;
public static class Parser {
private static char[] whitespace = { ' ', '\r', '\n', '\t' };
private static string[] keywords = {
+ "and",
+ "break",
+ "by",
+ "catch",
"class",
- "if",
- "extends",
- "new",
+ "continue",
+ "debugger",
+ "delete",
+ "do",
"else",
- "is",
- "isnt",
- "try",
- "catch",
+ "extends",
+ "false",
"finally",
"for",
+ "if",
"in",
- "of",
- "while",
- "until",
- "do",
- "then",
- "switch",
- "when",
+ "instanceof",
+ "is",
+ "isnt",
+ "loop",
+ "new",
+ "no",
"not",
- "and",
+ "null",
+ "of",
+ "off",
+ "on",
"or",
+ "return",
+ "super",
+ "switch",
+ "then",
+ "this",
+ "throw",
"true",
+ "try",
+ "typeof",
+ "undefined",
+ "unless",
+ "until",
+ "when",
+ "while",
"yes",
- "on",
- "false",
- "no",
- "off",
- "this",
};
private static Func<string, ParseResult>[] p =
{
ParseComment,
+ ParseAt,
ParseKeyword,
ParseNumericLiteral,
ParseIdentifier,
@@ -54,19 +69,26 @@ public static class Parser {
private static ParseResult ParseComment(string s) {
if (s[0] == '#') {
- return new ParseResult(s.Length, Token.Comment);
+ return new ParseResult(s.Length, Token.Comment, s);
}
return null;
}
+ private static ParseResult ParseAt(string s) {
+ if (s[0] == '@') {
+ return new ParseResult(1, Token.This, "@");
+ }
+ return null;
+ }
+
private static ParseResult ParseNumericLiteral(string s) {
if (!char.IsDigit(s[0])) {
return null;
}
int length = s
.TakeWhile(c => char.IsNumber(c) || c == '.' || c == 'e' || c == 'E')
.Count();
- return new ParseResult(length, Token.NumericLiteral);
+ return new ParseResult(length, Token.NumericLiteral, s.Substring(0, length));
}
private static ParseResult ParseIdentifier(string s) {
@@ -76,21 +98,22 @@ public static class Parser {
int length = s
.TakeWhile(c => char.IsLetterOrDigit(c) || c == '_')
.Count();
- return new ParseResult(length, Token.Identifier);
+ return new ParseResult(length, Token.Identifier, s.Substring(0, length));
}
- private static bool PredicateKeyword(string s) {
- return keywords
- .Any(x => s.Substring(0, Math.Min(x.Length,s.Length)) == x && (s.Length <= x.Length || whitespace.Contains(s[x.Length])));
- }
+ //private static bool PredicateKeyword(string s) {
+ // return keywords
+ // .Any(x => s.Substring(0, Math.Min(x.Length,s.Length)) == x && (s.Length <= x.Length || whitespace.Contains(s[x.Length])));
+ //}
private static ParseResult ParseKeyword(string s) {
- var keyword = keywords
- .FirstOrDefault(x => s.Substring(0, Math.Min(x.Length, s.Length)) == x && (s.Length <= x.Length || whitespace.Contains(s[x.Length])));
+ var keyword = keywords
+ .FirstOrDefault(x => s.Substring(0, Math.Min(x.Length, s.Length)) == x &&
+ (s.Length <= x.Length || whitespace.Contains(s[x.Length]) || s[x.Length].In('.', ',' , '[', '(', ';', ')', ']')));
if (keyword == null) {
return null;
}
- return new ParseResult(keyword.Length, Token.Keyword);
+ return new ParseResult(keyword.Length, Token.Keyword, keyword);
}
private static ParseResult ParseStringLiteral(string s) {
@@ -112,7 +135,7 @@ public static class Parser {
// Cope with unterminated strings
length = s.Length;
}
- return new ParseResult(length, Token.StringLiteral);
+ return new ParseResult(length, Token.StringLiteral, s.Substring(length));
}
return null;
}
@@ -124,16 +147,34 @@ public static class Parser {
while (s.Length > 0) {
var r = p.Select(x => x(s)).Where(x => x != null).FirstOrDefault();
if (r != null) {
- ret.Add(new TokenInfo(ofs, r.Item1, r.Item2, null));
+ ret.Add(new TokenInfo(ofs, r.Item1, r.Item2, r.Item3, null));
s = s.Substring(r.Item1);
ofs += r.Item1;
} else {
s = s.Substring(1);
ofs++;
}
}
- return ret;
+ return PostProcess(ret);
}
+ private static IEnumerable<TokenInfo> PostProcess(IEnumerable<TokenInfo> tokens) {
+ TokenInfo prevToken = null;
+ foreach (var token in tokens.Concat(new TokenInfo(-1, -1, Token.None, null, null))) {
+ if (prevToken != null) {
+ if (prevToken.Token == Token.This && token.Token == Token.Identifier) {
+ yield return new TokenInfo(prevToken.Start, prevToken.Length + token.Length, Token.This, prevToken.Value+token.Value, null);
+ } else if (prevToken.Token == Token.Keyword && prevToken.Value == "this") {
+ yield return new TokenInfo(prevToken.Start, prevToken.Length, Token.This, prevToken.Value, null);
+ } else {
+ if (prevToken.Token != Token.None) {
+ yield return prevToken;
+ }
+ }
+ }
+ prevToken = token;
+ }
+ }
+
}
}
View
@@ -6,11 +6,14 @@
namespace CoffeeParser {
public enum Token {
+ None = 0,
+
StringLiteral,
NumericLiteral,
Keyword,
Identifier,
Comment,
+ This,
}
}
Oops, something went wrong.

0 comments on commit 414b48a

Please sign in to comment.