diff --git a/scripts/bundle.js b/scripts/bundle.js index b1ad53af..531a17c2 100644 --- a/scripts/bundle.js +++ b/scripts/bundle.js @@ -49,6 +49,7 @@ bundleOne('ruby/ruby'); bundleOne('rust/rust'); bundleOne('scss/scss'); bundleOne('sql/sql'); +bundleOne('st/st'); bundleOne('swift/swift'); bundleOne('vb/vb'); bundleOne('xml/xml'); diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index d1c71f9c..340dddd3 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -39,6 +39,7 @@ import './sb/sb.contribution'; import './scss/scss.contribution'; import './solidity/solidity.contribution'; import './sql/sql.contribution'; +import './st/st.contribution'; import './swift/swift.contribution'; import './vb/vb.contribution'; import './xml/xml.contribution'; diff --git a/src/st/st.contribution.ts b/src/st/st.contribution.ts new file mode 100644 index 00000000..a30c2dc6 --- /dev/null +++ b/src/st/st.contribution.ts @@ -0,0 +1,17 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import { registerLanguage } from '../_.contribution'; + +// Allow for running under nodejs/requirejs in tests +const _monaco: typeof monaco = (typeof monaco === 'undefined' ? (self).monaco : monaco); + +registerLanguage({ + id: 'st', + extensions: ['.st'], + aliases: ['StructuredText', 'scl', 'stl'], + loader: () => _monaco.Promise.wrap(import('./st')) +}); diff --git a/src/st/st.test.ts b/src/st/st.test.ts new file mode 100644 index 00000000..cc8f8eba --- /dev/null +++ b/src/st/st.test.ts @@ -0,0 +1,133 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import { testTokenization } from '../test/testRunner'; +testTokenization('st', [ + [{ + line: "xVar : BOOL;", + tokens: [ + { startIndex: 0, type: 'identifier.st' }, + { startIndex: 4, type: 'white.st' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'white.st' }, + { startIndex: 7, type: 'type.st' }, + { startIndex: 11, type: 'delimiter.st' }, + ] + }], + [{ + line: "xStart AT %IX0.0.1: BOOL := TRUE;", + tokens: [ + { startIndex: 0, type: 'identifier.st' }, + { startIndex: 6, type: 'white.st' }, + { startIndex: 7, type: 'keyword.st' }, + { startIndex: 9, type: 'white.st' }, + { startIndex: 10, type: 'tag.st' }, + { startIndex: 18, type: '' }, + { startIndex: 19, type: 'white.st' }, + { startIndex: 20, type: 'type.st' }, + { startIndex: 24, type: 'white.st' }, + { startIndex: 25, type: '' }, + { startIndex: 27, type: 'white.st' }, + { startIndex: 28, type: 'constant.st' }, + { startIndex: 32, type: 'delimiter.st' }, + ] + }], + [{ + line: "IF a > 2#0000_0110 THEN (* Somethign ' happens *)", + tokens: [ + { startIndex: 0, type: 'keyword.st' }, + { startIndex: 2, type: 'white.st' }, + { startIndex: 3, type: 'identifier.st' }, + { startIndex: 4, type: 'white.st' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'white.st' }, + { startIndex: 7, type: 'number.binary.st' }, + { startIndex: 18, type: 'white.st' }, + { startIndex: 19, type: 'keyword.st' }, + { startIndex: 23, type: 'white.st' }, + { startIndex: 24, type: 'comment.st' }, + ] + }], + [{ + line: "TON1(IN := TRUE, PT := T#20ms, Q => xStart); // Run timer", + tokens: [ + { startIndex: 0, type: 'identifier.st' }, + { startIndex: 4, type: 'delimiter.parenthesis.st' }, + { startIndex: 5, type: 'identifier.st' }, + { startIndex: 7, type: 'white.st' }, + { startIndex: 8, type: '' }, + { startIndex: 10, type: 'white.st' }, + { startIndex: 11, type: 'constant.st' }, + { startIndex: 15, type: '' }, + { startIndex: 16, type: 'white.st' }, + { startIndex: 17, type: 'identifier.st' }, + { startIndex: 19, type: 'white.st' }, + { startIndex: 20, type: '' }, + { startIndex: 22, type: 'white.st' }, + { startIndex: 23, type: 'tag.st' }, + { startIndex: 29, type: '' }, + { startIndex: 30, type: 'white.st' }, + { startIndex: 31, type: 'identifier.st' }, + { startIndex: 32, type: 'white.st' }, + { startIndex: 33, type: '' }, + { startIndex: 35, type: 'white.st' }, + { startIndex: 36, type: 'identifier.st' }, + { startIndex: 42, type: 'delimiter.parenthesis.st' }, + { startIndex: 43, type: 'delimiter.st' }, + { startIndex: 44, type: 'white.st' }, + { startIndex: 45, type: 'comment.st' }, + ] + }], + // Numbers + [{ + line: '0', + tokens: [ + { startIndex: 0, type: 'number.st' } + ] + }], + + [{ + line: '0.0', + tokens: [ + { startIndex: 0, type: 'number.float.st' } + ] + }], + + [{ + line: '2#000_0101', + tokens: [ + { startIndex: 0, type: 'number.binary.st' } + ] + }], + [{ + line: '16#0f', + tokens: [ + { startIndex: 0, type: 'number.hex.st' } + ] + }], + + [{ + line: '23.5', + tokens: [ + { startIndex: 0, type: 'number.float.st' } + ] + }], + + [{ + line: '23.5e3', + tokens: [ + { startIndex: 0, type: 'number.float.st' } + ] + }], + + [{ + line: '23.5E3', + tokens: [ + { startIndex: 0, type: 'number.float.st' } + ] + }], +]); diff --git a/src/st/st.ts b/src/st/st.ts new file mode 100644 index 00000000..3a822f98 --- /dev/null +++ b/src/st/st.ts @@ -0,0 +1,178 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import IRichLanguageConfiguration = monaco.languages.LanguageConfiguration; +import ILanguage = monaco.languages.IMonarchLanguage; + +export const conf: IRichLanguageConfiguration = { + comments: { + lineComment: '//', + blockComment: ['(*', '*)'], + }, + brackets: [ + ['{', '}'], + ['[', ']'], + ['(', ')'] + ], + autoClosingPairs: [ + { open: '[', close: ']' }, + { open: '{', close: '}' }, + { open: '(', close: ')' }, + { open: '/*', close: '*/' }, + { open: '\'', close: '\'', notIn: ['string_sq'] }, + { open: '"', close: '"', notIn: ['string_dq'] }, + ], + surroundingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"' }, + { open: '\'', close: '\'' }, + ], + folding: { + markers: { + start: new RegExp("^\\s*#pragma\\s+region\\b"), + end: new RegExp("^\\s*#pragma\\s+endregion\\b") + } + } +}; + +export const language = { + defaultToken: '', + tokenPostfix: '.st', + ignoreCase: true, + + brackets: [ + { token: 'delimiter.curly', open: '{', close: '}' }, + { token: 'delimiter.parenthesis', open: '(', close: ')' }, + { token: 'delimiter.square', open: '[', close: ']' } + ], + + keywords: ['if', 'end_if', 'elsif', 'else', 'case', 'of', 'to', + 'do', 'with', 'by', 'while', 'repeat', 'end_while', 'end_repeat', 'end_case', + 'for', 'end_for', 'task', 'retain', 'non_retain', 'constant', 'with', 'at', + 'exit', 'return', 'interval', 'priority', 'address', 'port', 'on_channel', + 'then', 'iec', 'file', 'uses', 'version', 'packagetype', 'displayname', + 'copyright', 'summary', 'vendor', 'common_source', 'from'], + + constant: ['false', 'true', 'null'], + + defineKeywords: [ + 'var', 'var_input', 'var_output', 'var_in_out', 'var_temp', 'var_global', + 'var_access', 'var_external', 'end_var', + + 'type', 'end_type', 'struct', 'end_struct', 'program', 'end_program', + 'function', 'end_function', 'function_block', 'end_function_block', + + 'configuration', 'end_configuration', 'tcp', 'end_tcp', 'recource', + 'end_recource', 'channel', 'end_channel', 'library', 'end_library', + 'folder', 'end_folder', 'binaries', 'end_binaries', 'includes', + 'end_includes', 'sources', 'end_sources', + + 'action', 'end_action', 'step', 'initial_step', 'end_step', 'transaction', 'end_transaction' + ], + + typeKeywords: ['int', 'sint', 'dint', 'lint', 'usint', 'uint', 'udint', 'ulint', + 'real', 'lreal', 'time', 'date', 'time_of_day', 'date_and_time', 'string', + 'bool', 'byte', 'world', 'dworld', 'array', 'pointer', 'lworld'], + + operators: ['=', '>', '<', ':', ':=', '<=', '>=', '<>', '&', '+', '-', '*', '**', + 'MOD', '^', 'or', 'and', 'not', 'xor', 'abs', 'acos', 'asin', 'atan', 'cos', + 'exp', 'expt', 'ln', 'log', 'sin', 'sqrt', 'tan', 'sel', 'max', 'min', 'limit', + 'mux', 'shl', 'shr', 'rol', 'ror', 'indexof', 'sizeof', 'adr', 'adrinst', + 'bitadr', 'is_valid'], + + builtinVariables: [ + + ], + + builtinFunctions: ['sr', 'rs', 'tp', 'ton', 'tof', 'eq', 'ge', 'le', 'lt', + 'ne', 'round', 'trunc', 'ctd', 'сtu', 'ctud', 'r_trig', 'f_trig', + 'move', 'concat', 'delete', 'find', 'insert', 'left', 'len', 'replace', + 'right', 'rtc'], + + // we include these common regular expressions + symbols: /[=>