Skip to content

Commit

Permalink
update checking to warn of specific errors; modify code alignment for…
Browse files Browse the repository at this point in the history
… flake8; add Apache default license to SM code (to do: make configurable); update cmake_minimum_version
  • Loading branch information
David Conner committed Jul 19, 2023
1 parent 7074324 commit c9c9c03
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.10.2)
project(flexbe_app)

find_package(ament_cmake REQUIRED)
Expand Down
28 changes: 22 additions & 6 deletions src/_helper/checking.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,19 @@ Checking = new (function() {

var close_stack = [];
var dot_last = false;

for (var i = 0; i < expr.length; i++) {
var c = expr[i];
if (dot_last && !c.match(/[a-z_0-9]/i)) return false;
if (dot_last && !c.match(/[A-Z,a-z_ 0-9]/i) && !closing.contains(c)) {
T.logError("Invalid expression <"+expr+"> : either number, letter, comma, space, underscore, or closing delimiter must follow dot.\n");
return false;
}
else dot_last = false;
if (c == "," && close_stack.length == 0) return false;

if (c == "," && close_stack.length == 0)
{
T.logError("Invalid expression <"+expr+"> : comma encountered while not in delimiters!\n");
return false;
}
if (close_stack.length > 0 && c == close_stack[close_stack.length - 1]) {
close_stack.pop();
continue;
Expand All @@ -294,16 +301,25 @@ Checking = new (function() {
}
if (c == "#") {
if (allow_comment) break;
else return false;
else {
T.logError("Invalid expression <"+expr+"> : comments using # are not allowed here.\n");
return false;
}
}
if (c == '.') {
dot_last = true;
continue;
}
if (closing.contains(c)) return false;
if (closing.contains(c)) {
T.logError("Invalid expression <"+expr+"> : character '" + c + " in closing without opening.\n");
return false;
}
}
}
if (close_stack.length > 0) return false;
if (close_stack.length > 0) {
T.logError("Invalid expression <"+expr+"> : missing final delimiter!\n");
return false;
}

return true;
}
Expand Down
52 changes: 38 additions & 14 deletions src/io/io_codegenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ IO.CodeGenerator = new (function() {
var state_init_list = [];

var ws = ' '; // flake8 does not want tabs

var wss = ' '; // shorter for SM.add alignment
var autonomyMapping = function(autonomy_int) {
switch(parseInt(autonomy_int)) {
case 0: return "Autonomy.Off";
Expand Down Expand Up @@ -93,6 +93,24 @@ IO.CodeGenerator = new (function() {
return code;
}

var generateLicenseText = function() {
var code = "";
code += '# Copyright ' + Behavior.getCreationDate() + " " + Behavior.getAuthor() + "\n";
code += '#' + "\n";
code += '# Licensed under the Apache License, Version 2.0 (the "License");' + "\n";
code += '# you may not use this file except in compliance with the License.' + "\n";
code += '# You may obtain a copy of the License at' + "\n";
code += '#' + "\n";
code += '# http://www.apache.org/licenses/LICENSE-2.0' + "\n";
code += '#' + "\n";
code += '# Unless required by applicable law or agreed to in writing, software' + "\n";
code += '# distributed under the License is distributed on an "AS IS" BASIS,' + "\n";
code += '# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.' + "\n";
code += '# See the License for the specific language governing permissions and' + "\n";
code += '# limitations under the License.' + "\n";
return code;
}

var generateClassDefinition = function() {
var code = "";
code += "class " + names.class_name + "(Behavior):\n";
Expand Down Expand Up @@ -155,7 +173,7 @@ IO.CodeGenerator = new (function() {
code += ws+ws+"# Additional initialization code can be added inside the following tags\n";
code += ws+ws+"# [MANUAL_INIT]";
if (Behavior.getManualCodeInit() == "") {
code += "\n"+ws+ws+"\n"+ws+ws;
code += "\n\n"+ws+ws;
} else {
code += Behavior.getManualCodeInit();
}
Expand Down Expand Up @@ -209,7 +227,7 @@ IO.CodeGenerator = new (function() {
code += ws+ws+"# Additional creation code can be added inside the following tags\n";
code += ws+ws+"# [MANUAL_CREATE]";
if (Behavior.getManualCodeCreate() == "") {
code += "\n"+ws+ws+"\n"+ws+ws;
code += "\n\n"+ws+ws;
} else {
code += Behavior.getManualCodeCreate();
}
Expand All @@ -221,7 +239,7 @@ IO.CodeGenerator = new (function() {
sub_sms.sort(compareKey(sm => sm.getStatePath()));
for (var i = sub_sms.length - 1; i >= 0; i--) {
code += generateStateMachine(sub_sms[i], true);
code += "\n";
code += ""; // flake8 only skip one line in main method
}

// generate root state machine
Expand All @@ -238,7 +256,7 @@ IO.CodeGenerator = new (function() {
code += ws+"# Private functions can be added inside the following tags\n";
code += ws+"# [MANUAL_FUNC]";
if (Behavior.getManualCodeFunc() == "") {
code += "\n"+ws+"\n"+ws;
code += "\n\n"+ws;
} else {
code += Behavior.getManualCodeFunc();
}
Expand Down Expand Up @@ -340,7 +358,7 @@ IO.CodeGenerator = new (function() {
var sm_name = sm_names.findElement(function (element) {
return element.sm.getStatePath() == s.getStatePath();
}).name;
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+ sm_name + ",\n";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+ sm_name + ",\n";

} else if (s instanceof BehaviorState) {
var defkeys_str = "";
Expand All @@ -350,7 +368,7 @@ IO.CodeGenerator = new (function() {
be_defkeys_str.push("'"+s.getInputKeys()[j]+"'");
}
if (be_defkeys_str.length > 0) {
defkeys_str = ",\n"+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"default_keys=[" + be_defkeys_str.join(',') + "]";
defkeys_str = ",\n"+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"default_keys=[" + be_defkeys_str.join(',') + "]";
}
var params_str = "";
var be_params_str = [];
Expand All @@ -359,15 +377,15 @@ IO.CodeGenerator = new (function() {
be_params_str.push("'"+s.getParameters()[j]+"': "+s.getParameterValues()[j]);
}
if (be_params_str.length > 0) {
params_str = ",\n"+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"parameters={" + be_params_str.join(', ') + "}";
params_str = ",\n"+ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"parameters={" + be_params_str.join(', ') + "}";
}
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"self.use_behavior(" + s.getStateClass() + ", '" + s.getStatePath().substr(1) + "'" + defkeys_str + params_str + "),\n";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"self.use_behavior(" + s.getStateClass() + ", '" + s.getStatePath().substr(1) + "'" + defkeys_str + params_str + "),\n";

} else {
var class_key = (!UI.Settings.isExplicitStates() && WS.Statelib.isClassUnique(s.getStateClass()))?
s.getStateClass() :
s.getStatePackage() + "__" + s.getStateClass();
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+ class_key + "(";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+ class_key + "(";
var param_strings = [];
for (var j=0; j<s.getParameters().length; ++j) {
if (s.getParameters()[j].startsWith("?")) continue;
Expand All @@ -378,7 +396,7 @@ IO.CodeGenerator = new (function() {
}

// transitions
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"transitions={";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"transitions={";
var transition_strings = [];
var state_transitions = t.filter(function (element) {
return element.getFrom().getStateName() == s.getStateName();
Expand All @@ -397,7 +415,7 @@ IO.CodeGenerator = new (function() {
code += "},\n";

// autonomy
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"autonomy={";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"autonomy={";
var autonomy_strings = [];
for (var j=0; j<s.getOutcomes().length; ++j) {
autonomy_strings.push("'" + s.getOutcomes()[j] + "': " + autonomyMapping(s.getAutonomy()[j]));
Expand All @@ -418,7 +436,7 @@ IO.CodeGenerator = new (function() {
}
if (remapping_strings.length > 0) {
code += ",\n";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+ws+"remapping={";
code += ws+ws+ws+ws+ws+ws+ws+ws+ws+wss+"remapping={";
code += remapping_strings.join(", ");
code += "}";
}
Expand All @@ -438,11 +456,17 @@ IO.CodeGenerator = new (function() {
sm_counter = 0;
sm_names = [];
ws = UI.Settings.getCodeIndentation();
console.log('Using whitespace: "'+ws+'"')
if (ws != ' ') {
wss = ws; // no specific alignment policy except for 4 spaces
}
T.logInfo('Using whitespace: "'+ws+'" and "' + wss + '"')

// prefix
var code = "#!/usr/bin/env python\n";
code += "# -*- coding: utf-8 -*-\n";
code += "\n";
code += generateLicenseText();
code += "\n";
code += "###########################################################\n";
code += "# WARNING: Generated code! #\n";
code += "# ************************** #\n";
Expand Down

0 comments on commit c9c9c03

Please sign in to comment.