Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove inline comments in the preprocessor #74

Merged
merged 3 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions models/comments/comments.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
a[A1]
0 0
a[A2]
0 1
a[A3]
0 2
b
0 3
c
0 4
d
0 8760
e
0 42
1 42
FINAL TIME
0 1
INITIAL TIME
0 0
SAVEPER
0 1
TIME STEP
0 1
53 changes: 53 additions & 0 deletions models/comments/comments.mdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{UTF-8}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say that it would be nice if we had a way to verify that the mdl output of the preprocessor doesn't contain the comments, but then I remembered that I added such a thing to the test harness a few months ago. I added a check script for the comments test and the output mdl file is as expected, so I'll push that to this branch shortly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed that script in b42e664 and verified that the build passes.

DimA: A1, A2, A3 ~~|

a[DimA]=
0, {transportation sector}
1, {electricity sector}
2 {geoengineering sector - NOT USED}
~
~ ~ :SUPPLEMENTARY
|

b = 3 {transportation sector}
~
~ ~ :SUPPLEMENTARY
|

c = 4
{first part and
the second part}
~
~ ~ :SUPPLEMENTARY
|

d=
8760

{
We ignore leap years so that we can get consistent results
for various values used in the model that should not change
every four years. If we want to change this value for leap
years, instead use the following code:


IF THEN ELSE(
(MODULO(Time, 4) = 0 :AND: MODULO(Time, 100) <> 0) :OR: MODULO(Time, 400) = 0,
8784 {leap year},
8760 {normal year}
)
}
~ hours
~ Calculates the number of hours in the year, ignoring leap years.
~ :SUPPLEMENTARY
|

e = 41 {first part} + 1 {second part}
~
~ ~ :SUPPLEMENTARY
|

INITIAL TIME = 0 ~~|
FINAL TIME = 1 ~~|
SAVEPER = 1 ~~|
TIME STEP = 1 ~~|
69 changes: 69 additions & 0 deletions models/comments/comments_vars.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
a[DimA]: const (non-apply-to-all)
= 0,1,2
refId(_a[_a1])
families(_dima)
subscripts(_a1)
separationDims(_dima)
hasInitValue(false)

a[DimA]: const (non-apply-to-all)
= 0,1,2
refId(_a[_a2])
families(_dima)
subscripts(_a2)
separationDims(_dima)
hasInitValue(false)

a[DimA]: const (non-apply-to-all)
= 0,1,2
refId(_a[_a3])
families(_dima)
subscripts(_a3)
separationDims(_dima)
hasInitValue(false)

b: const
= 3
refId(_b)
hasInitValue(false)

c: const
= 4
refId(_c)
hasInitValue(false)

d: const
= 8760
refId(_d)
hasInitValue(false)

e: const
= 41+1
refId(_e)
hasInitValue(false)

FINAL TIME: const
= 1
refId(_final_time)
hasInitValue(false)

INITIAL TIME: const
= 0
refId(_initial_time)
hasInitValue(false)

SAVEPER: const
= 1
refId(_saveper)
hasInitValue(false)

Time: const
=
refId(_time)
hasInitValue(false)

TIME STEP: const
= 1
refId(_time_step)
hasInitValue(false)

30 changes: 29 additions & 1 deletion src/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const sh = require('shelljs')
const split = require('split-string')
const byline = require('byline')
const XLSX = require('xlsx')
const num = require('numbro')
const B = require('bufx')

// Set true to print a stack trace in vlog
Expand Down Expand Up @@ -379,6 +378,34 @@ let matchRegexCaptures = (str, regex) => {
return []
}
}
// Match delimiters recursively. Replace delimited strings globally.
let replaceDelimitedStrings = (str, open, close, newStr) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (from inspection and from running the test) that this works as advertised, but (note to selves) at some point we should set up Jest so that we can have more isolated unit tests for these sorts of helper functions. Not a blocking thing; just something that's been on my mind for a long time but haven't had time to work on.

// str is the string to operate on.
// open and close are the opening and closing delimiter characters.
// newStr is the string to replace delimited substrings with.
let result = ''
let start = 0
let depth = 0
let n = str.length
for (let i = 0; i < n; i++) {
if (str.charAt(i) === open) {
if (depth === 0) {
result += str.substring(start, i)
}
depth++
} else if (str.charAt(i) === close && depth > 0) {
depth--
if (depth === 0) {
result += newStr
start = i + 1
}
}
}
if (start < n) {
result += str.substring(start)
}
return result
}

/**
* Return the cartesian product of the given array of arrays.
Expand Down Expand Up @@ -474,6 +501,7 @@ module.exports = {
readDat,
readXlsx,
replaceInArray,
replaceDelimitedStrings,
rest,
splitEquations,
strings,
Expand Down
4 changes: 3 additions & 1 deletion src/Preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const R = require('ramda')
const B = require('bufx')
const { splitEquations } = require('./Helpers')
const { splitEquations, replaceDelimitedStrings } = require('./Helpers')

let preprocessModel = (mdlFilename, spec, profile = 'genc', writeFiles = false, outDecls = []) => {
const MACROS_FILENAME = 'macros.txt'
Expand Down Expand Up @@ -134,6 +134,8 @@ let preprocessModel = (mdlFilename, spec, profile = 'genc', writeFiles = false,
eqn = eqn.replace('{UTF-8}', '')
// Remove ":RAW:" flag; it is not needed by SDE and causes problems if left in
eqn = eqn.replace(/:RAW:/g, '')
// Remove inline comments
eqn = replaceDelimitedStrings(eqn, '{', '}', '')
// Remove whitespace
eqn = eqn.trim()
if (eqn.length > 0) {
Expand Down