Skip to content

Commit

Permalink
Merge pull request #203 from fujaba/feat/number-names
Browse files Browse the repository at this point in the history
Numbers in names
  • Loading branch information
Clashsoft committed Oct 4, 2020
2 parents b178514 + 79e3b14 commit ad8b9a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/antlr/ScenarioParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ it: IT;
answer: THE? ANSWER;

simpleName: identifier;
name: identifier+;
name: identifier (identifier | INTEGER)*;

identifier: WORD
// new keywords for pattern matching since v1.1
Expand Down
40 changes: 13 additions & 27 deletions src/main/java/org/fulib/scenarios/parser/Identifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.fulib.StrUtil;
import org.fulib.scenarios.ast.decl.Name;
Expand All @@ -25,61 +26,46 @@ public static String toLowerCamelCase(String text)
return StrUtil.downFirstChar(toUpperCamelCase(text));
}

public static Stream<String> splitCaps(String text)
private static Stream<String> splitCaps(String text)
{
return Arrays.stream(text.split("[\\W_]+"));
}

public static String joinCaps(Stream<String> stream)
private static String joinCaps(Stream<String> stream)
{
return stream.map(StrUtil::cap).collect(Collectors.joining());
}

// --------------- Parser-Specific ---------------

static String cap(Token token)
static String joinCaps(ScenarioParser.SimpleNameContext simpleName)
{
return StrUtil.cap(token.getText());
return joinCaps(splitCaps(simpleName.identifier().getText()));
}

static Stream<String> splitCaps(TerminalNode terminalNode)
static String joinCaps(ScenarioParser.NameContext name)
{
return splitCaps(terminalNode.getText());
return joinCaps(name.children.stream().map(ParseTree::getText).flatMap(Identifiers::splitCaps));
}

static Stream<String> splitCaps(Token token)
static String varName(ScenarioParser.SimpleNameContext simpleName)
{
return splitCaps(token.getText());
return simpleName == null ? null : StrUtil.downFirstChar(joinCaps(simpleName));
}

static String joinCaps(ScenarioParser.SimpleNameContext context)
static String varName(ScenarioParser.NameContext name)
{
return joinCaps(splitCaps(context.identifier().getStart()));
}

static String joinCaps(ScenarioParser.NameContext context)
{
return joinCaps(context.identifier().stream().map(ScenarioParser.IdentifierContext::getStart).flatMap(Identifiers::splitCaps));
}

static String varName(ScenarioParser.NameContext context)
{
return context == null ? null : StrUtil.downFirstChar(joinCaps(context));
}

static String varName(ScenarioParser.SimpleNameContext context)
{
return context == null ? null : StrUtil.downFirstChar(joinCaps(context));
return name == null ? null : StrUtil.downFirstChar(joinCaps(name));
}

static Name name(ScenarioParser.SimpleNameContext simpleName)
{
return simpleName == null ? null : name(varName(simpleName), simpleName);
}

static Name name(ScenarioParser.NameContext multiName)
static Name name(ScenarioParser.NameContext name)
{
return multiName == null ? null : name(varName(multiName), multiName);
return name == null ? null : name(varName(name), name);
}

private static Name name(String value, ParserRuleContext rule)
Expand Down
24 changes: 16 additions & 8 deletions src/test/scenarios/language/identifiers/Identifiers.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Scenario Identifiers.

There is a CamelCase.
There is a lowerCamelCase.
There is a Kebab-Case.
There is a lower-kebab-case.
There is a Snake_Case.
There is a lower_snake_case.
# Identifiers

## Case Conversions

There is the object CamelCase.
There is the object lowerCamelCase.
There is the object Kebab-Case.
There is the object lower-kebab-case.
There is the object Snake_Case.
There is the object lower_snake_case.

## Numbers

There is the object example 1.
There is the object example 2 three 4.
There is the object example 5 6 7.

0 comments on commit ad8b9a8

Please sign in to comment.