Skip to content

Commit

Permalink
icb
Browse files Browse the repository at this point in the history
  • Loading branch information
Traineratwot committed Jan 4, 2024
1 parent 016203c commit c7d009d
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 973 deletions.
125 changes: 125 additions & 0 deletions core/icB.SemanticProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import vscode, {CancellationToken, ProviderResult, SemanticTokens, TextDocument} from "vscode"
import * as fs from "fs";

// import * as fs from "fs";

interface IParsedToken {
line: number;
startCharacter: number;
length: number;
tokenType: number;
tokenModifier?: number
}

const tokenTypes = new Map<string, number>()
const tokenModifiers = new Map<string, number>()
export const legendIcB = (function () {
const tokenTypesLegend = [
"parameter", "keyword", "enumMember",
"property", "function",
"variable", "label"
]
tokenTypesLegend.forEach((tokenType, index) => tokenTypes.set(tokenType, index))

const tokenModifiersLegend = [
"declaration", "readonly"
]
tokenModifiersLegend.forEach((tokenModifier, index) => tokenModifiers.set(tokenModifier, index))

return new vscode.SemanticTokensLegend(tokenTypesLegend, tokenModifiersLegend)
})()

export class IcBSemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {

provideDocumentSemanticTokens(document: TextDocument, token: CancellationToken): ProviderResult<SemanticTokens> {
const allTokens = this._parseText(document.getText())
const builder = new vscode.SemanticTokensBuilder(legendIcB)
allTokens.forEach((token) => {
builder.push(
token.line, token.startCharacter, token.length,
token.tokenType,
)
})
return builder.build()
}

_parseText(text: string): IParsedToken[] {
try {
let r: IParsedToken[] = []
const lines = text.split(/\r\n|\r|\n/)
const vars: string[] = []
const keywords: string[] = []
const constants: string[] = []

lines.forEach((line) => {
let match
try {
let re = /\b(var|alias)\s+([\w\d]+).*/
if (re.test(line)) {
match = re.exec(line)
vars.push(match[2])
}
re = /\b(const|define)\s+([\w\d]+).*/
if (re.test(line)) {
match = re.exec(line)
constants.push(match[2])
}
re = /([\w\d]+):/
if (re.test(line)) {
match = re.exec(line)
keywords.push(match[1])
}
} catch (e) {
}
})
console.table(vars)
fs.writeFileSync('C:\\Projects\\IC\\vscode-stationeers-ic10\\core\\Test.json',JSON.stringify(vars))

lines.forEach((line, index) => {
try {
for (let value of vars) {
r = this.pushToken(value, line, index, 0, null, r)
}
for (let value of keywords) {
r = this.pushToken(value, line, index, 1, null, r)
}
for (let value of constants) {
r = this.pushToken(value, line, index, 2, 1, r)
}
} catch (e) {
}
})
return r
} catch (e) {
}
return []
}

pushToken(search, line, index, tokenType, tokenModifier, out: IParsedToken[]) {
const find = new RegExp("\\b" + search + "\\b", "y")
try {
for (let i = 0; i < line.length; i++) {
if (line[i] == "#") {
break
}
find.lastIndex = i
const match = find.exec(line)
if (match && match[0] == search) {
const a: IParsedToken = {
line: index,
startCharacter: match.index,
length: search.length,
tokenType: tokenType,
}
if (tokenModifier !== null) {
a.tokenModifier = tokenModifier
}
out.push(a)
}
}
} catch (e) {
}
return out
}

}
2 changes: 2 additions & 0 deletions core/icX.SemanticProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vscode, {CancellationToken, ProviderResult, SemanticTokens, TextDocument} from "vscode"
import fs from "fs";

// import * as fs from "fs";

Expand Down Expand Up @@ -93,6 +94,7 @@ export class IcxSemanticTokensProvider implements vscode.DocumentSemanticTokensP
}

pushToken(search, line, index, tokenType, tokenModifier, out: IParsedToken[]) {

const find = new RegExp("\\b" + search + "\\b", "y")
try {
for (let i = 0; i < line.length; i++) {
Expand Down
23 changes: 13 additions & 10 deletions core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Ic10Vscode} from "./ic10-vscode"
import path from "path"
import {ic10Formatter} from "./ic10.formatter"
import {IcxSemanticTokensProvider, legend} from "./icX.SemanticProvider"
import {IcBSemanticTokensProvider, legendIcB} from "./icB.SemanticProvider"
import {Ic10SidebarViewProvider} from "./sidebarView"
import {ic10Diagnostics} from "./ic10.diagnostics"
import {icX} from "icx-compiler"
Expand All @@ -15,13 +16,15 @@ import {IcXVscode} from "./icX-vscode"
import InterpreterIc10 from "ic10"
import {Ic10Error} from "ic10/src/Ic10Error"
import {parseEnvironment} from "../debugger/utils";
import fs from "fs";


const LOCALE_KEY: string = vscode.env.language
const ic10_hover = new Ic10Vscode()
const icX_hover = new IcXVscode()
export const LANG_IC10 = "ic10"
export const LANG_ICX = "icX"
export const LANG_ICB = "icB"
const interpreterIc10 = new InterpreterIc10(null)
let interpreterIc10State = 0
let leftCodeLength: vscode.StatusBarItem
Expand Down Expand Up @@ -278,12 +281,12 @@ html,body,iframe{
}

function semantic(ctx: vscode.ExtensionContext) {
// console.time('semantic')
console.time('semantic')
try {
ctx.subscriptions.push(vscode.languages.registerDocumentSemanticTokensProvider(
{language: LANG_IC10, scheme: "file"},
new IcxSemanticTokensProvider,
legend
{language: LANG_ICB, scheme: "file"},
new IcBSemanticTokensProvider,
legendIcB
)
)
ctx.subscriptions.push(vscode.languages.registerDocumentSemanticTokensProvider(
Expand All @@ -293,9 +296,9 @@ function semantic(ctx: vscode.ExtensionContext) {
)
)
} catch (e) {
// console.error(e)
console.error(e)
}
// console.timeEnd('semantic')
console.timeEnd('semantic')
}

function view(ctx: vscode.ExtensionContext) {
Expand Down Expand Up @@ -358,15 +361,15 @@ function statusBar(ctx: vscode.ExtensionContext) {
// console.timeEnd('statusBar')
}

function ChangeActiveTextEditor(editor): void {
function ChangeActiveTextEditor(editor:vscode.TextEditor): void {
if (vscode.window.activeTextEditor.document.languageId == LANG_IC10 || vscode.window.activeTextEditor.document.languageId == LANG_ICX) {
onChangeCallbacks.ChangeActiveTextEditor.forEach((e) => {
e.call(null, editor)
})
}
}

function ChangeTextEditorSelection(editor): void {
function ChangeTextEditorSelection(editor:vscode.TextEditorSelectionChangeEvent): void {
if (vscode.window.activeTextEditor.document.languageId == LANG_IC10 || vscode.window.activeTextEditor.document.languageId == LANG_ICX) {

onChangeCallbacks.ChangeTextEditorSelection.forEach((e) => {
Expand All @@ -383,7 +386,7 @@ function SaveTextDocument(): void {
}
}

function onChange(ctx) {
function onChange(ctx:vscode.ExtensionContext) {
ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(ChangeActiveTextEditor))
ctx.subscriptions.push(vscode.workspace.onDidSaveTextDocument(SaveTextDocument))
ctx.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(ChangeTextEditorSelection))
Expand All @@ -404,7 +407,7 @@ function getNumberLeftLines(): Array<any> | false {
}
}

function diagnostic(context) {
function diagnostic(context:vscode.ExtensionContext) {
// console.time('diagnostic')

try {
Expand Down
96 changes: 96 additions & 0 deletions icB.language-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"comments": {
"lineComment": "#"
},
"brackets": [
[
"IF",
"ENDIF"
],
[
"{",
"}"
],
[
"{",
"}"
],
[
"{",
"}"
],
[
"[",
"]"
],
[
"(",
")"
]
],
"autoClosingPairs": [

{
"open": "{",
"close": "}"
},
{
"open": "[",
"close": "]"
},
{
"open": "(",
"close": ")"
},
{
"open": "'",
"close": "'",
"notIn": [
"string",
"comment"
]
},
{
"open": "\"",
"close": "\"",
"notIn": [
"string"
]
},
{
"open": "`",
"close": "`",
"notIn": [
"string",
"comment"
]
},
],
"autoCloseBefore": ";:.,=}])>` \n\t",
"surroundingPairs": [
[
"{",
"}"
],
[
"[",
"]"
],
[
"(",
")"
],
[
"'",
"'"
],
[
"\"",
"\""
],
[
"`",
"`"
]
]
}
30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"contributes": {
"files.associations": {
"*.ic10": "ic10",
"*.icX": "icX"
"*.icX": "icX",
"*.icb": "icB",
"*.icB": "icB"
},
"languages": [
{
Expand All @@ -68,6 +70,16 @@
"icX"
],
"configuration": "./icX.language-configuration.json"
},
{
"id": "icB",
"aliases": [
"icB"
],
"extensions": [
"icb"
],
"configuration": "./icB.language-configuration.json"
}
],
"grammars": [
Expand All @@ -80,6 +92,11 @@
"language": "icX",
"scopeName": "source.icX",
"path": "./syntaxes/icX.tmLanguage.json"
},
{
"language": "icB",
"scopeName": "source.icB",
"path": "./syntaxes/icB.tmLanguage.json"
}
],
"debuggers": [
Expand Down Expand Up @@ -252,14 +269,6 @@
}
]
},
"configurationDefaults": {
"ic10": {
"editor.semanticHighlighting.enabled": true
},
"icX": {
"editor.semanticHighlighting.enabled": true
}
},
"views": {
"explorer": [
{
Expand All @@ -278,6 +287,9 @@
},
"icX": {
"editor.semanticHighlighting.enabled": true
},
"icB": {
"editor.semanticHighlighting.enabled": true
}
},
"dependencies": {
Expand Down
Loading

0 comments on commit c7d009d

Please sign in to comment.