-
-
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.
For the moment I am using one file for each verb (e.g. read.ts and connect.ts). The result is that connect is a parser that matches only one sentence.
- Loading branch information
1 parent
76a8d3e
commit 0add251
Showing
2 changed files
with
102 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { createToken, Lexer, CstParser } from "@slangroom/deps/chevrotain"; | ||
import { JsonableObject } from "@slangroom/shared"; | ||
import { StmtContext } from "@slangroom/core/slangroom"; | ||
import { Whitespace, Identifier } from "@slangroom/shared/tokens" | ||
import { Web3 } from 'web3' | ||
|
||
type EthereumConnect = { | ||
address: string | ||
} | ||
|
||
const Ethereum = createToken({ | ||
name: "Ethereum", | ||
pattern: /ethereum/i, | ||
}); | ||
const The = createToken({ | ||
name: "The", | ||
pattern: /the/i, | ||
group: Lexer.SKIPPED, | ||
}); | ||
const Provider = createToken({ | ||
name: "Provider", | ||
pattern: /Provider/i, | ||
}); | ||
const To = createToken({ | ||
name: "To", | ||
pattern: /To/i, | ||
}); | ||
|
||
const allTokens = [ | ||
Whitespace, | ||
The, | ||
To, | ||
Ethereum, | ||
Provider, | ||
Identifier, | ||
]; | ||
const StatementLexer = new Lexer(allTokens); | ||
// ----------------- parser ----------------- | ||
class StatementParser extends CstParser { | ||
constructor() { | ||
super(allTokens); | ||
|
||
this.performSelfAnalysis(); | ||
} | ||
|
||
public statement = this.RULE("statement", () => { | ||
this.CONSUME(To); | ||
this.CONSUME(Ethereum); | ||
this.CONSUME(Provider); | ||
this.CONSUME(Identifier); | ||
}); | ||
} | ||
|
||
const parser = new StatementParser(); | ||
// ----------------- Interpreter ----------------- | ||
const BaseCstVisitor = parser.getBaseCstVisitorConstructor(); | ||
|
||
class StatementInterpreter extends BaseCstVisitor { | ||
constructor() { | ||
super(); | ||
this.validateVisitor(); | ||
} | ||
statement(ctx: any) { | ||
return {address: ctx.Identifier[0].image.slice(1,-1)} | ||
} | ||
} | ||
|
||
// We only need a single interpreter instance because our interpreter has no state. | ||
const interpreter = new StatementInterpreter(); | ||
|
||
|
||
export const line2Ast = (text: string) => { | ||
const lexResult = StatementLexer.tokenize(text); | ||
parser.input = lexResult.tokens; | ||
const cst = parser.statement(); | ||
const value = interpreter.visit(cst); | ||
return { | ||
value: value, | ||
lexResult: lexResult, | ||
parseErrors: parser.errors, | ||
}; | ||
} | ||
|
||
type EthereumConnectResult = { | ||
name: string, | ||
value: Web3 | ||
} | ||
|
||
export const evaluate = async (ast: EthereumConnect, | ||
keys: JsonableObject, stmtCtx: StmtContext): Promise<EthereumConnectResult> => { | ||
const address = (keys[ast.address] || stmtCtx.data[ast.address] || ast.address) as string | ||
return { | ||
name: "web3", | ||
value: new Web3(address) | ||
} | ||
} |
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