Skip to content

Commit

Permalink
feat: added monarch syntax for demo editor
Browse files Browse the repository at this point in the history
  • Loading branch information
liana-p committed Oct 15, 2023
1 parent ae0c850 commit 327f938
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/narrat-editor/src/components/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ onMounted(() => {
const scriptToEdit = gameStore.activeScript;
if (scriptToEdit) {
editor.value = monaco.editor.create(editorContainer.value!, {
language: 'python',
language: 'narrat',
value: scriptToEdit.code,
automaticLayout: true,
theme: 'vs-dark',
Expand Down
165 changes: 165 additions & 0 deletions packages/narrat-editor/src/monaco/narrat-monarch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Difficulty: "Moderate"
// Python language definition.
// Only trickiness is that we need to check strings before identifiers
// since they have letter prefixes. We also treat ':' as an @open bracket
// in order to get auto identation.
export const narratMonarchLanguage = {
defaultToken: '',
tokenPostfix: '.python',

keywords: [
'set',
'var',
'talk',
'think',
'jump',
'run',
'wait',
'return',
'save',
'save_prompt',
'log',
'clear_dialog',
'set_screen',
'empty_layer',
'set_button',
'play',
'pause',
'stop',
'notify',
'enable_notifications',
'disable_notifications',
'set_stat',
'get_stat_value',
'add_stat',
'neg',
'abs',
'random',
'random_float',
'random_from_args',
'min',
'max',
'clamp',
'floor',
'round',
'ceil',
'sqrt',
'^',
'concat',
'join',
'text_field',
'add_level',
'set_level',
'add_xp',
'roll',
'get_level',
'get_xp',
'add_item',
'remove_item',
'enable_interaction',
'disable_interaction',
'has_item?',
'item_amount?',
'start_quest',
'start_objective',
'complete_objective',
'complete_quest',
'quest_started',
'objective_started?',
'quest_completed?',
'objective_completed?',
// Other keywords
'if',
'choice',
'elseif',
'else',
'&&',
'||',
'!=',
'==',
'>=',
'<=',
'<',
'>',
'!',
'+',
'-',
'*',
'/',
],

brackets: [
{ open: '{', close: '}', token: 'delimiter.curly' },
{ open: '[', close: ']', token: 'delimiter.bracket' },
{ open: '(', close: ')', token: 'delimiter.parenthesis' },
],

tokenizer: {
root: [
{ include: '@whitespace' },
{ include: '@numbers' },
{ include: '@strings' },

[/[,:;]/, 'delimiter'],
[/[{}\[\]()]/, '@brackets'],

[/\$[a-zA-Z]\w*/, 'tag'],
[
/[a-zA-Z]\w*/,
{
cases: {
'@keywords': 'keyword',
'@default': 'identifier',
},
},
],
],

// Deal with white space, including single and multi-line comments
whitespace: [
[/\s+/, 'white'],
[/(^\/\/.*$)/, 'comment'],
[/('''.*''')|(""".*""")/, 'string'],
[/'''.*$/, 'string', '@endDocString'],
[/""".*$/, 'string', '@endDblDocString'],
],
endDocString: [
[/\\'/, 'string'],
[/.*'''/, 'string', '@popall'],
[/.*$/, 'string'],
],
endDblDocString: [
[/\\"/, 'string'],
[/.*"""/, 'string', '@popall'],
[/.*$/, 'string'],
],

// Recognize hex, negatives, decimals, imaginaries, longs, and scientific notation
numbers: [
[/-?0x([abcdef]|[ABCDEF]|\d)+[lL]?/, 'number.hex'],
[/-?(\d*\.)?\d+([eE][+\-]?\d+)?[jJ]?[lL]?/, 'number'],
],

// Recognize strings, including those broken across lines with \ (but not without)
strings: [
[/'$/, 'string.escape', '@popall'],
[/'/, 'string.escape', '@stringBody'],
[/"$/, 'string.escape', '@popall'],
[/"/, 'string.escape', '@dblStringBody'],
],
stringBody: [
[/[^\\']+$/, 'string', '@popall'],
[/[^\\']+/, 'string'],
[/\\./, 'string'],
[/'/, 'string.escape', '@popall'],
[/\\$/, 'string'],
],
dblStringBody: [
[/[^\\"]+$/, 'string', '@popall'],
[/[^\\"]+/, 'string'],
[/\\./, 'string'],
[/"/, 'string.escape', '@popall'],
[/\\$/, 'string'],
],
},
};
5 changes: 5 additions & 0 deletions packages/narrat-editor/src/monaco/setup-monaco.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import './setup-yaml';
import './setup-workers';
import * as monaco from 'monaco-editor';
import { narratMonarchLanguage } from './narrat-monarch';

monaco.languages.register({ id: 'narrat' });
monaco.languages.setMonarchTokensProvider('narrat', narratMonarchLanguage);
2 changes: 2 additions & 0 deletions packages/narrat-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"allowJs": true,
"noImplicitAny": false,

/* Linting */
"strict": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/narrat/src/components/debug/debug-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import { resetSave } from '@/utils/save-helpers';
import { vm } from '@/vm/vm';
import DebugJumping from './debug-jumping.vue';
import { InputListener } from '@/stores/inputs-store';
import { useRenderingStore } from '@/lib';
import { useRenderingStore } from '@/stores/rendering-store';
export default defineComponent({
components: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AlertModal from '../alert-modal.vue';
import { createPinia, setActivePinia } from 'pinia';
import cloneDeep from 'clone-deep';
import { mockConfig } from '@/tests/mock-config';
import { useConfig } from '@/lib';
import { useConfig } from '@/stores/config-store';

describe('AlertModal.vue test', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/narrat/src/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rpgGame from '@/examples/rpg/scripts';
import emptyGame from '@/examples/empty/scripts';
import godotGame from '@/examples/godot/scripts';
import { NarratScript } from '@/types/app-types';
import { registerPlugin } from '@/lib';
import { registerPlugin } from '@/exports/plugins';
import { GodotPlugin } from '@/plugins/godot-plugin';

const gameScripts: Record<string, NarratScript[]> = {
Expand Down
2 changes: 2 additions & 0 deletions packages/narrat/src/hmr/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useMain } from '@/stores/main-store';
import { useNotifications } from '@/stores/notification-store';
import { NarratScript } from '@/types/app-types';
import { vm } from '@/vm/vm';
import { ModuleNamespace } from 'vite/types/hot';

export function handleHMR(newModule: ModuleNamespace | undefined) {
useMain().clearErrors();
if (!newModule || !newModule.default) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/narrat/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { gameloop } from '@/utils/gameloop';
import { getSaveFile } from './utils/save-helpers';
import { ModuleNamespace } from 'vite/types/hot';
import { constructNarratObject } from './utils/construct-narrat';
import { useRenderingStore } from './lib';
import { useRenderingStore } from './stores/rendering-store';
import cloneDeep from 'clone-deep';
let app: any;

Expand Down

0 comments on commit 327f938

Please sign in to comment.