Permalink
Browse files

Work on automatic generation of field names.

  • Loading branch information...
1 parent 35dcfe9 commit 8711534c724dcd6acbaf120eb9ba5610586217ac @VladD2 VladD2 committed Jul 13, 2012
View
@@ -25,4 +25,7 @@ Thumbs.db
obj/
[Rr]elease*/
_ReSharper*/
-[Tt]est[Rr]esult*
+[Tt]est[Rr]esult*
+Common/Performance1.psess
+N2VsExtentions/Performance1.psess
+*.vsp
@@ -23,22 +23,62 @@ namespace Nemerle.Parser.Model
| Ast
{
names : list[NameRef];
+ [Memoize]
public GetNames(rd : RuleDefinition) : list[NameRef]
{
def makeName(name : NameRef, rule : Rule) : NameRef
{
+ def toCamelCase(str : string, sufix : string = null) : string
+ {
+ if (str.Length == 0)
+ str
+ else if (sufix == null)
+ char.ToUpperInvariant(str[0]).ToString() + str.Substring(1).ToLowerInvariant()
+ else
+ sufix + char.ToUpperInvariant(str[0]).ToString() + str.Substring(1).ToLowerInvariant()
+ }
+ def toCamelCaseName(name : NameRef) : NameRef
+ {
+ def str = name.AsName;
+ if (str.Length == 0 || char.IsUpper(str[0]))
+ name
+ else
+ NameRef(name.Location, toCamelCase(str))
+ }
+ def makeKeywordName(str : string) : string
+ {
+ def isIdentifier(str : string) : bool
+ {
+ if (str.Length == 0)
+ false
+ else
+ {
+ def ch = str[0];
+
+ if (ch == '_' || char.IsLetter(ch))
+ str.Exists(ch => ch == '_' || char.IsLetterOrDigit(ch)) // TODO: Performance problem
+ else
+ false
+ }
+ }
+
+ if (isIdentifier(str))
+ toCamelCase(str, "Kw")
+ else
+ Message.FatalError(rule.Location, $<#Name for literal "$str" not defined. You should define by 'token field Name = String;' statement.#>)
+ }
def makeNameForRule(rule : Rule) : NameRef
{
| String(str) when rd.TokenFieldNames.Contains(str) => rd.TokenFieldNames.Get(str)
- | String(str) => Message.FatalError(rule.Location, $<#Name for literal "$str" not defined. You should define by 'token field Name = String;' statement.#>)
- | Call(name, _) => name
+ | String(str) => NameRef(name.Location, makeKeywordName(str))
+ | Call(name, _) => toCamelCaseName(name)
| _ => Message.FatalError(rule.Location, $<#Name for rule "$rule" not defined.#>)
}
match (name.Parts)
{
| ["_"] => makeNameForRule(rule)
- | [_] => name
+ | [_] => name
| _ => assert2(false); assert(false);
}
}
@@ -21,7 +21,6 @@ namespace Nemerle.Parser
public GrammarStateTB : TypeBuilder;
public GrammarDescriptorImplTB : TypeBuilder;
public ParsingErrorsTB : TypeBuilder;
- //public TokenFieldNames : Map[string, NameRef];
[Accessor]
private _manager : ManagerClass;
@@ -41,13 +40,11 @@ namespace Nemerle.Parser
private _astType : FixedType;
private _ntokenType : FixedType;
- public this(manager : ManagerClass, env : GlobalEnv, tb : TypeBuilder, parsedGrammar : Grammar)//, tokenFieldNames : Map[string, NameRef])
+ public this(manager : ManagerClass, env : GlobalEnv, tb : TypeBuilder, parsedGrammar : Grammar)
{
when (!(tb.Attributes %&& NemerleModifiers.Abstract))
Message.FatalError(tb.NameLocation, "Class must be abstract.");
- //TokenFieldNames = tokenFieldNames;
-
GrammarTB = tb;
GrammarImplTB = GrammarTB.DefineNestedType(MakeGrammarImpl());
GrammarDescriptorImplTB = GrammarImplTB.DefineNestedType(MakeGrammarDescriptorImpl());
@@ -37,7 +37,6 @@ namespace Nemerle.Parser
public Imports : list[Ast] { get; }
public GrammarCompiler : GrammarCompiler { get; internal set; }
public TypeBuilder : TypeBuilder { get; }
- //public TokenFieldNames : Map[string, NameRef] { get; }
}
internal class N2SourceImpl
@@ -317,7 +316,6 @@ namespace Nemerle.Parser
grammar = grammar.Add(ruleDecl);
| G.TokenField.Ast(StringLiteral=G.StringLiteral.Ast as str) as decl =>
- //assert2(false);
tokenFieldNames = tokenFieldNames.Replace(str.GetText(NToken(str.Open.EndPos, str.Close.StartPos)), decl.Name.ToNameRef());
| G.SpanClass.Ast as decl =>
@@ -342,7 +340,7 @@ namespace Nemerle.Parser
}
]>);
- SyntaxModule(name, grammar, imports.NToList(), null, typeBuilder)//, tokenFieldNames)
+ SyntaxModule(name, grammar, imports.NToList(), null, typeBuilder)
}
public ConvertAttributes(attributes : G.RuleAttributes) : list[RuleAttribute]

0 comments on commit 8711534

Please sign in to comment.