Skip to content

Commit

Permalink
fix: correct generated code when model variable name includes parenth…
Browse files Browse the repository at this point in the history
…eses (#585)

Fixes #582
  • Loading branch information
chrispcampbell authored Dec 8, 2024
1 parent 5549c2a commit 25412f2
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 197 deletions.
198 changes: 16 additions & 182 deletions models/flatten/expected.dat
Original file line number Diff line number Diff line change
@@ -1,87 +1,15 @@
" quotes 2 with extra whitespace "
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
"Quotes 1"
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Apple 1
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Apple 2
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
apple 3
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Banana 1
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Banana 2
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
banana 3
0 1
1 1
Expand All @@ -96,124 +24,28 @@ banana 3
10 1
carrot 1
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
carrot 2
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Equation with backslashes[A1]
0 2
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2
Equation with backslashes[A2]
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Equation with backslashes[A3]
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Equation without backslashes[A1]
0 2
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2
Equation without backslashes[A2]
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Equation without backslashes[A3]
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
FINAL TIME
0 10
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
INITIAL TIME
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
Quotes 1
0 1
"Quotes 3 with parentheses (1) lorem ipsum"
0 1
SAVEPER
0 1
1 1
Expand All @@ -228,16 +60,6 @@ SAVEPER
10 1
TIME STEP
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
Using equation with backslashes[A1]
0 4
1 4
Expand Down Expand Up @@ -310,3 +132,15 @@ Using equation without backslashes[A3]
8 2
9 2
10 2
Using lookup
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
6 changes: 6 additions & 0 deletions models/flatten/expected.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ Look2((0,0),(1,1),(2,2)) ~~|

" quotes 2 with extra whitespace " = 1 ~~|

"Quotes 3 with parentheses (1) lorem ipsum" = 1 ~~|

"Quotes 3 with parentheses (2) lookup" ((0,0),(1,1)) ~~|

SAVEPER = TIME STEP ~~|

TIME STEP = 1 ~~|

Using equation with backslashes[DimA] = Equation with backslashes[DimA] * 2 ~~|

Using equation without backslashes[DimA] = Equation without backslashes[DimA] * 2 ~~|

Using lookup = "Quotes 3 with parentheses (2) lookup" (0) ~~|
15 changes: 15 additions & 0 deletions models/flatten/input1.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
~ Comment
|

"Quotes 3 with parentheses (1) lorem ipsum" = 1
~ dmnl
~ Comment
|

"Quotes 3 with parentheses (2) lookup" ((0,0),(1,1))
~ dmnl
~ Comment
|

Using lookup = "Quotes 3 with parentheses (2) lookup" (0)
~ dmnl
~ Comment
|

apple 3 = 1
~ dmnl
~ Comment
Expand Down
2 changes: 1 addition & 1 deletion packages/parse/src/_shared/canonical-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const reWhitespace = new RegExp('(\\s|_)+', 'g')
// Detect special punctuation characters
// TODO: We do not currently include '!' characters in this set; we should only replace these
// when they don't appear at the end of a (marked) dimension
const reSpecialChars = new RegExp(`['"\\.,\\-\\$&%\\/\\|]`, 'g')
const reSpecialChars = new RegExp(`['"\\.,\\-\\$&%\\/\\|()]`, 'g')

/**
* Format a model variable or subscript/dimension name into a valid C identifier (with
Expand Down
5 changes: 2 additions & 3 deletions packages/parse/src/_shared/canonical-id.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ describe('canonicalId', () => {
add('slash', '/')
// TODO: Handle backslashes
// add('bslash', '\\')
// TODO: Handle parentheses
// add('lparen', '(')
// add('rparen', ')')
add('lparen', '(')
add('rparen', ')')
input += ' characters"'
expected += '_characters_'
expect(canonicalId(input)).toBe(expected)
Expand Down
32 changes: 27 additions & 5 deletions packages/parse/src/vensim/preprocess-vensim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Vsmooth = INTEG((input - Vsmooth)/SMOOTH TIME, input)
:END OF MACRO:
X[A] = 1 ~~| Y[B] = 2 ~~|
Z[A,B] =
Z[ A, B ] =
X[A] +
Y[B]
~ EJ/year
Expand All @@ -48,7 +48,11 @@ Another to remove = 3 ~~|
Group comment here.
|
W[A,B] :EXCEPT: [A1,B1] = 1 ~~|
W[ A,B] :EXCEPT: [ A1 , B1 ] = 1 ~~|
"Quoted with parentheses (1) lorem ipsum" = 1 ~~|
"Quoted with __ parentheses (2) lookup" ((0,0),(1,1)) ~~|
\\\\\\---/// Sketch information - do not modify anything except names
V301 Do not put anything below this section - it will be ignored
Expand Down Expand Up @@ -94,20 +98,38 @@ $192-192-192,0,Arial|12||0-0-0|0-0-0|0-0-255|-1--1--1|-1--1--1|96,96,5,0
},
{
key: 'z[a,b]',
def: 'Z[A,B] = X[A] + Y[B] ~~|',
def: 'Z[ A, B ] = X[A] + Y[B] ~~|',
kind: 'eqn',
line: 15,
units: 'EJ/year',
comment: 'Comment text is here. And here. Here, too. And on a second line.'
},
{
key: 'w[a,b] :except: [a1,b1]',
def: 'W[A,B] :EXCEPT: [A1,B1] = 1 ~~|',
key: 'w[a,b]_:except:_[a1,b1]',
def: 'W[ A,B] :EXCEPT: [ A1 , B1 ] = 1 ~~|',
kind: 'eqn',
line: 42,
units: '',
comment: '',
group: 'Group name'
},
{
key: 'quoted_with_parentheses_(1)_lorem_ipsum',
def: '"Quoted with parentheses (1) lorem ipsum" = 1 ~~|',
kind: 'eqn',
line: 44,
units: '',
comment: '',
group: 'Group name'
},
{
key: 'quoted_with_parentheses_(2)_lookup',
def: '"Quoted with __ parentheses (2) lookup" ((0,0),(1,1)) ~~|',
kind: 'decl',
line: 46,
units: '',
comment: '',
group: 'Group name'
}
],
removedMacros: [
Expand Down
Loading

0 comments on commit 25412f2

Please sign in to comment.