-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
884 additions
and
884 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
import { Language } from "./language"; | ||
import vscodeLangDetection from '@vscode/vscode-languagedetection'; | ||
const { ModelOperations } = vscodeLangDetection; | ||
|
||
export class DetectLanguage { | ||
private static extensionToLanguageMap = new Map<string, Language>([ | ||
['c', Language.C], | ||
['cpp', Language.CPP], | ||
['cs', Language.CSharp], | ||
['go', Language.GO], | ||
['java', Language.JAVA], | ||
['js', Language.JAVASCRIPT], | ||
['php', Language.PHP], | ||
['py', Language.PYTHON], | ||
['rb', Language.RUBY], | ||
['ts', Language.TYPESCRIPT] | ||
]); | ||
|
||
static fromExtension(extension: string): Language { | ||
const language = this.extensionToLanguageMap.get(extension.toLowerCase()); | ||
if (!language) { | ||
throw new Error(`Unsupported language extension: ${extension}`); | ||
} | ||
return language; | ||
} | ||
|
||
static async fromSourceCode(sourceCode: string): Promise<Language> { | ||
const modelOperations = new ModelOperations(); | ||
|
||
try { | ||
const result = await modelOperations.runModel(sourceCode); | ||
|
||
const mostConfidentLanguage = result.reduce((prev, current) => { | ||
return (prev.confidence > current.confidence) ? prev : current; | ||
}); | ||
|
||
return this.fromExtension(mostConfidentLanguage.languageId); | ||
|
||
} catch (error) { | ||
console.error('Error running detector model:', error); | ||
throw new Error('Failed to detect language'); // Optionally throw an error if needed | ||
} | ||
} | ||
|
||
static fromAntlrContext(ctx: any): Language { | ||
const constructor_name = ctx.constructor.name.toLowerCase(); | ||
if (constructor_name.includes('python')) { | ||
return Language.PYTHON; | ||
} else if (constructor_name.includes('java')) { | ||
return Language.JAVA; | ||
} | ||
|
||
throw new Error(`Unsupported language: ${constructor_name}`); | ||
} | ||
import { Language } from "./language"; | ||
import vscodeLangDetection from '@vscode/vscode-languagedetection'; | ||
const { ModelOperations } = vscodeLangDetection; | ||
|
||
export class DetectLanguage { | ||
private static extensionToLanguageMap = new Map<string, Language>([ | ||
['c', Language.C], | ||
['cpp', Language.CPP], | ||
['cs', Language.CSharp], | ||
['go', Language.GO], | ||
['java', Language.JAVA], | ||
['js', Language.JAVASCRIPT], | ||
['php', Language.PHP], | ||
['py', Language.PYTHON], | ||
['rb', Language.RUBY], | ||
['ts', Language.TYPESCRIPT] | ||
]); | ||
|
||
static fromExtension(extension: string): Language { | ||
const language = this.extensionToLanguageMap.get(extension.toLowerCase()); | ||
if (!language) { | ||
throw new Error(`Unsupported language extension: ${extension}`); | ||
} | ||
return language; | ||
} | ||
|
||
static async fromSourceCode(sourceCode: string): Promise<Language> { | ||
const modelOperations = new ModelOperations(); | ||
|
||
try { | ||
const result = await modelOperations.runModel(sourceCode); | ||
|
||
const mostConfidentLanguage = result.reduce((prev, current) => { | ||
return (prev.confidence > current.confidence) ? prev : current; | ||
}); | ||
|
||
return this.fromExtension(mostConfidentLanguage.languageId); | ||
|
||
} catch (error) { | ||
console.error('Error running detector model:', error); | ||
throw new Error('Failed to detect language'); // Optionally throw an error if needed | ||
} | ||
} | ||
|
||
static fromAntlrContext(ctx: any): Language { | ||
const constructor_name = ctx.constructor.name.toLowerCase(); | ||
if (constructor_name.includes('python')) { | ||
return Language.PYTHON; | ||
} else if (constructor_name.includes('java')) { | ||
return Language.JAVA; | ||
} | ||
|
||
throw new Error(`Unsupported language: ${constructor_name}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
export enum Language { | ||
C, | ||
CPP, | ||
CSharp, | ||
GO, | ||
JAVA, | ||
JAVASCRIPT, | ||
PYTHON, | ||
PHP, | ||
RUBY, | ||
TYPESCRIPT, | ||
UNKNOWN | ||
export enum Language { | ||
C, | ||
CPP, | ||
CSharp, | ||
GO, | ||
JAVA, | ||
JAVASCRIPT, | ||
PYTHON, | ||
PHP, | ||
RUBY, | ||
TYPESCRIPT, | ||
UNKNOWN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { ICodeBlock } from "./interface/ICodeBlock"; | ||
|
||
import { parsePythonSource } from './language/python/parser'; | ||
import { parseJavaSource } from './language/java/parser'; | ||
|
||
import * as fs from 'fs'; | ||
import { JavaCodeBlock } from './language/java'; | ||
import { PyCodeBlock } from './language/python'; | ||
|
||
export class ParseSource { | ||
static fromFile(filePath: string): ICodeBlock { | ||
const extension = filePath.split('.').pop(); | ||
|
||
if(!extension) { | ||
throw new Error('No valid file extension'); | ||
} | ||
|
||
const text = fs.readFileSync(filePath, 'utf-8'); | ||
return ParseSource.fromText(text, extension); | ||
} | ||
|
||
static fromText(text: string, extension: string): JavaCodeBlock | PyCodeBlock { | ||
switch(extension) { | ||
case 'java': | ||
return new JavaCodeBlock(parseJavaSource(text)); | ||
case 'py': | ||
return new PyCodeBlock(parsePythonSource(text)); | ||
default: | ||
throw new Error('Unsupported language'); | ||
} | ||
} | ||
import { ICodeBlock } from "./interface/ICodeBlock"; | ||
|
||
import { parsePythonSource } from './language/python/parser'; | ||
import { parseJavaSource } from './language/java/parser'; | ||
|
||
import * as fs from 'fs'; | ||
import { JavaCodeBlock } from './language/java'; | ||
import { PyCodeBlock } from './language/python'; | ||
|
||
export class ParseSource { | ||
static fromFile(filePath: string): ICodeBlock { | ||
const extension = filePath.split('.').pop(); | ||
|
||
if(!extension) { | ||
throw new Error('No valid file extension'); | ||
} | ||
|
||
const text = fs.readFileSync(filePath, 'utf-8'); | ||
return ParseSource.fromText(text, extension); | ||
} | ||
|
||
static fromText(text: string, extension: string): JavaCodeBlock | PyCodeBlock { | ||
switch(extension) { | ||
case 'java': | ||
return new JavaCodeBlock(parseJavaSource(text)); | ||
case 'py': | ||
return new PyCodeBlock(parsePythonSource(text)); | ||
default: | ||
throw new Error('Unsupported language'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ICodeBlock } from './ICodeBlock'; | ||
import { IMethod } from './IMethod'; | ||
|
||
export interface IClass extends ICodeBlock { | ||
name: string; | ||
getMethods(): IMethod[]; | ||
import { ICodeBlock } from './ICodeBlock'; | ||
import { IMethod } from './IMethod'; | ||
|
||
export interface IClass extends ICodeBlock { | ||
name: string; | ||
getMethods(): IMethod[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Language } from "../language"; | ||
|
||
export abstract class ICodeBlock { | ||
ctx: any; | ||
language?: Language; | ||
import { Language } from "../language"; | ||
|
||
export abstract class ICodeBlock { | ||
ctx: any; | ||
language?: Language; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ICodeBlock } from './ICodeBlock'; | ||
import { IParameter } from './IParameter'; | ||
|
||
export interface IFunction extends ICodeBlock{ | ||
name: string; | ||
getParameters(): IParameter[]; | ||
import { ICodeBlock } from './ICodeBlock'; | ||
import { IParameter } from './IParameter'; | ||
|
||
export interface IFunction extends ICodeBlock{ | ||
name: string; | ||
getParameters(): IParameter[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { IParameter } from './IParameter'; | ||
import { IFunction } from './IFunction'; | ||
|
||
import { IParameter } from './IParameter'; | ||
import { IFunction } from './IFunction'; | ||
|
||
export interface IMethod extends IFunction {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
export interface IParameter { | ||
name: string; | ||
type: string | null; | ||
|
||
export interface IParameter { | ||
name: string; | ||
type: string | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { IFunction } from '../IFunction'; | ||
import { IMethod } from '../IMethod'; | ||
|
||
export interface IMethodLength { | ||
calculate(method: IMethod | IFunction): number; | ||
import { IFunction } from '../IFunction'; | ||
import { IMethod } from '../IMethod'; | ||
|
||
export interface IMethodLength { | ||
calculate(method: IMethod | IFunction): number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { IFunction } from '../IFunction'; | ||
import { IMethod } from '../IMethod'; | ||
|
||
export interface ILongMethod { | ||
detect(method: IFunction | IMethod): boolean; | ||
import { IFunction } from '../IFunction'; | ||
import { IMethod } from '../IMethod'; | ||
|
||
export interface ILongMethod { | ||
detect(method: IFunction | IMethod): boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { JavaCodeBlockBase } from "./JavaCodeBlockBase"; | ||
import { JavaMethod } from "./JavaMethod"; | ||
import { NormalClassDeclarationContext, MethodDeclarationContext } from "../../../grammars-v4/java/java20/Java20Parser"; | ||
import JavaParserVisitor from "../../../grammars-v4/java/java20/Java20ParserVisitor"; | ||
|
||
export class JavaClass extends JavaCodeBlockBase<NormalClassDeclarationContext> { | ||
constructor(ctx: NormalClassDeclarationContext) { | ||
super(ctx); | ||
} | ||
|
||
get name(): string { | ||
return this.ctx.typeIdentifier().getText(); | ||
} | ||
|
||
getMethods(): JavaMethod[] { | ||
class MethodVisitor extends JavaParserVisitor<void> { | ||
methods: JavaMethod[]; | ||
constructor() { | ||
super(); | ||
this.methods = []; | ||
} | ||
|
||
visitMethodDeclaration = (ctx: MethodDeclarationContext) => { | ||
this.methods.push(new JavaMethod(ctx)); | ||
} | ||
} | ||
|
||
const visitor = new MethodVisitor(); | ||
visitor.visit(this.ctx); | ||
return visitor.methods; | ||
} | ||
import { JavaCodeBlockBase } from "./JavaCodeBlockBase"; | ||
import { JavaMethod } from "./JavaMethod"; | ||
import { NormalClassDeclarationContext, MethodDeclarationContext } from "../../../grammars-v4/java/java20/Java20Parser"; | ||
import JavaParserVisitor from "../../../grammars-v4/java/java20/Java20ParserVisitor"; | ||
|
||
export class JavaClass extends JavaCodeBlockBase<NormalClassDeclarationContext> { | ||
constructor(ctx: NormalClassDeclarationContext) { | ||
super(ctx); | ||
} | ||
|
||
get name(): string { | ||
return this.ctx.typeIdentifier().getText(); | ||
} | ||
|
||
getMethods(): JavaMethod[] { | ||
class MethodVisitor extends JavaParserVisitor<void> { | ||
methods: JavaMethod[]; | ||
constructor() { | ||
super(); | ||
this.methods = []; | ||
} | ||
|
||
visitMethodDeclaration = (ctx: MethodDeclarationContext) => { | ||
this.methods.push(new JavaMethod(ctx)); | ||
} | ||
} | ||
|
||
const visitor = new MethodVisitor(); | ||
visitor.visit(this.ctx); | ||
return visitor.methods; | ||
} | ||
} |
Oops, something went wrong.