Skip to content

Commit c49e4a9

Browse files
committed
feat: add support for constant checks
1 parent 2bf6582 commit c49e4a9

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

example.scl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ VERSION : 0.1
1919
test : UInt;
2020
END_VAR
2121

22+
VAR_GLOBAL CONSTANT
23+
MAX_HEIGHT : INT := 100;
24+
MAX_SAFE_HEIGHT : INT := MAX_HEIGHT - 5;
25+
counter : INT := 2
26+
END_VAR
27+
28+
VAR_GLOBAL
29+
DEFAULT : INT := 2
30+
errors : INT := 0
31+
END_VAR
32+
2233
VAR_TEMP
2334
messageTextIdx : UInt;
2435
source : String[10];

internal/models/style/symbol.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ func IsCamelCase(input string) bool {
1414
expression := regexp.MustCompile(`^[a-z]+([A-Z][a-z]*)*$`)
1515
return expression.MatchString(input)
1616
}
17+
18+
func IsMixedCapitalSnakeCase(input string) bool {
19+
expression := regexp.MustCompile(`^[A-Z]+([A-Z_][0-9]*)*$`)
20+
return expression.MatchString(input)
21+
}

main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const (
1919
)
2020

2121
var arguments []string = os.Args
22-
var parserIsInsideVariableBlock bool = false
2322
var isErrorDetected bool = false
2423
var variableBlockStartStrings = []string{"VAR_INPUT", "VAR", "VAR_OUTPUT", "VAR_TEMP", "VAR_GLOBAL"}
2524
var currentBlock Block = None
@@ -28,14 +27,24 @@ func checkVariableStyle(symbol string) {
2827
if style.IsMixedCamelCase(symbol) {
2928
fmt.Printf("Success: Variable %s is camel case.\n", symbol)
3029
} else {
31-
fmt.Fprintf(os.Stderr, "Error: Variable %s is not camel case.\n", symbol)
30+
fmt.Fprintf(os.Stderr, "Error: Variable %s is not mixed camel case.\n", symbol)
31+
isErrorDetected = true
32+
}
33+
}
34+
35+
func checkConstantStyle(symbol string) {
36+
if style.IsMixedCapitalSnakeCase(symbol) {
37+
fmt.Printf("Success: Constant %s is mixed capital snake case.\n", symbol)
38+
} else {
39+
fmt.Fprintf(os.Stderr, "Error: Constant %s is not mixed capital snake case.\n", symbol)
3240
isErrorDetected = true
3341
}
3442
}
3543

3644
func handleLine(input string) {
3745
if strings.TrimSpace(input) == "VAR_GLOBAL CONSTANT" {
3846
currentBlock = Constant
47+
return
3948
}
4049
inputWithoutWhiteSpaces := strings.ReplaceAll(input, " ", "")
4150
if currentBlock == Variable {
@@ -51,6 +60,14 @@ func handleLine(input string) {
5160
}
5261
}
5362

63+
if currentBlock == Constant {
64+
if strings.Contains(inputWithoutWhiteSpaces, ":") {
65+
parts := strings.Split(inputWithoutWhiteSpaces, ":")
66+
checkConstantStyle(parts[0])
67+
return
68+
}
69+
}
70+
5471
if slices.Contains(variableBlockStartStrings, inputWithoutWhiteSpaces) {
5572
if currentBlock == None {
5673
currentBlock = Variable

main_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ import (
55
)
66

77
func resetState() {
8-
parserIsInsideVariableBlock = false
98
currentBlock = None
109
}
1110

1211
func TestDetectsWhenInVariableBlock(test *testing.T) {
1312
test.Cleanup(resetState)
1413
handleLine("errorActive1 : Bool;")
15-
if parserIsInsideVariableBlock {
14+
if currentBlock == Variable {
1615
test.Error("mixed camel case must be valid")
1716
}
1817
}
1918

2019
func TestMixedCamelCaseVariableIsValid(test *testing.T) {
2120
test.Cleanup(resetState)
22-
parserIsInsideVariableBlock = true
2321
currentBlock = Variable
2422
handleLine("errorActive1 : Bool;")
2523
if isErrorDetected {
@@ -29,7 +27,6 @@ func TestMixedCamelCaseVariableIsValid(test *testing.T) {
2927

3028
func TestMixedCamelCaseFunctionIsValid(test *testing.T) {
3129
test.Cleanup(resetState)
32-
parserIsInsideVariableBlock = true
3330
currentBlock = Variable
3431
handleLine("almError {InstructionName := 'Program_Alarm'; LibVersion := '1.0'} : Program_Alarm;")
3532
if isErrorDetected {
@@ -39,19 +36,28 @@ func TestMixedCamelCaseFunctionIsValid(test *testing.T) {
3936

4037
func TestVariableBlockInitiallyNotDetected(test *testing.T) {
4138
test.Cleanup(resetState)
42-
if parserIsInsideVariableBlock || currentBlock == Constant || currentBlock == Variable {
39+
if currentBlock == Constant || currentBlock == Variable {
4340
test.Error("initial value of inside variable block should be false")
4441
}
4542
}
4643

47-
func TestSnakeCaseConstantsAreValid(test *testing.T) {
44+
func TestConstantBlockRecognized(test *testing.T) {
4845
test.Cleanup(resetState)
4946
handleLine("VAR_GLOBAL CONSTANT")
5047
if currentBlock != Constant {
5148
test.Error("did not detect that VAR_GLOBAL CONSTANT started")
5249
}
5350
}
5451

52+
func TestSnakeCaseConstantsAreValid(test *testing.T) {
53+
test.Cleanup(resetState)
54+
handleLine("VAR_GLOBAL CONSTANT")
55+
handleLine(" MAX_HEIGHT : INT := 100;")
56+
if isErrorDetected == true {
57+
test.Error("constants should be in mixed capital snake case")
58+
}
59+
}
60+
5561
func TestDoubleOpeningVariableBlockLeadsToError(test *testing.T) {
5662
test.Cleanup(resetState)
5763
handleLine("VAR_GLOBAL")

sps-buddy

-208 KB
Binary file not shown.

0 commit comments

Comments
 (0)