@@ -27,3 +27,11 @@ $ swift package swift package --disable-sandbox preview-documentation --target
|
|||||||
```
|
```
|
||||||
|
|
||||||
For more information, see the [documentation for the Swift-DocC plugin](https://swiftlang.github.io/swift-docc-plugin/documentation/swiftdoccplugin/).
|
For more information, see the [documentation for the Swift-DocC plugin](https://swiftlang.github.io/swift-docc-plugin/documentation/swiftdoccplugin/).
|
||||||
|
|
||||||
|
#### Checking Format
|
||||||
|
|
||||||
|
To check the format:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ swift package plugin --allow-writing-to-package-directory swiftformat
|
||||||
|
```
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
public struct Scope<T>: CustomStringConvertible {
|
public struct Scope<T>: CustomStringConvertible {
|
||||||
var symbols: Dictionary<Identifier, T> = Dictionary()
|
var symbols: [Identifier: T] = Dictionary()
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -28,10 +28,8 @@ public struct Scope<T>: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var count: Int {
|
public var count: Int {
|
||||||
get {
|
|
||||||
symbols.count
|
symbols.count
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public func lookup(identifier: Identifier) -> T? {
|
public func lookup(identifier: Identifier) -> T? {
|
||||||
if let symbol = symbols[identifier] {
|
if let symbol = symbols[identifier] {
|
||||||
@@ -79,10 +77,8 @@ public struct Scopes<T>: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var current: Scope<T>? {
|
public var current: Scope<T>? {
|
||||||
get {
|
|
||||||
scopes.last
|
scopes.last
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
||||||
var scopes = self.scopes
|
var scopes = self.scopes
|
||||||
@@ -120,8 +116,6 @@ public struct Scopes<T>: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var count: Int {
|
public var count: Int {
|
||||||
get {
|
|
||||||
scopes.count
|
scopes.count
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -82,7 +82,8 @@ public struct RequireErrorResult: ExpressionMacro {
|
|||||||
let expected_error = node.argumentList[arguments.startIndex].expression
|
let expected_error = node.argumentList[arguments.startIndex].expression
|
||||||
let error_producer = node.argumentList[arguments.index(after: arguments.startIndex)].expression
|
let error_producer = node.argumentList[arguments.index(after: arguments.startIndex)].expression
|
||||||
|
|
||||||
return ExprSyntax("""
|
return ExprSyntax(
|
||||||
|
"""
|
||||||
{
|
{
|
||||||
let __expected_error = \(expected_error)
|
let __expected_error = \(expected_error)
|
||||||
let __actual_error = \(error_producer)
|
let __actual_error = \(error_producer)
|
||||||
@@ -97,7 +98,6 @@ public struct RequireErrorResult: ExpressionMacro {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct P4Macros: CompilerPlugin {
|
struct P4Macros: CompilerPlugin {
|
||||||
var providingMacros: [Macro.Type] = [
|
var providingMacros: [Macro.Type] = [
|
||||||
|
|||||||
@@ -226,7 +226,6 @@ public struct ParserStates {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public struct Parser: P4Type {
|
public struct Parser: P4Type {
|
||||||
public var states: ParserStates
|
public var states: ParserStates
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ protocol ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension TypedIdentifier: ParseableEvaluatableExpression {
|
extension TypedIdentifier: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -61,7 +62,8 @@ extension TypedIdentifier: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4BooleanValue: ParseableEvaluatableExpression {
|
extension P4BooleanValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -101,7 +103,8 @@ extension P4BooleanValue: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4IntValue: ParseableEvaluatableExpression {
|
extension P4IntValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -132,7 +135,8 @@ extension P4IntValue: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4StringValue: ParseableEvaluatableExpression {
|
extension P4StringValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -202,4 +206,3 @@ extension ExpressionStatement: ParseableStatement {
|
|||||||
return Result.Ok((.none, scopes))
|
return Result.Ok((.none, scopes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,8 +70,10 @@ extension VariableDeclarationStatement: ParseableStatement {
|
|||||||
if rvalue.type().eq(rhs: declaration_p4_type) {
|
if rvalue.type().eq(rhs: declaration_p4_type) {
|
||||||
return Result.Ok(
|
return Result.Ok(
|
||||||
(
|
(
|
||||||
VariableDeclarationStatement(identifier: Identifier(name: variable_name), withInitializer: rvalue),
|
VariableDeclarationStatement(
|
||||||
scopes.declare(identifier: Identifier(name: variable_name), withValue: declaration_p4_type)
|
identifier: Identifier(name: variable_name), withInitializer: rvalue),
|
||||||
|
scopes.declare(
|
||||||
|
identifier: Identifier(name: variable_name), withValue: declaration_p4_type)
|
||||||
))
|
))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -98,7 +98,6 @@ public struct ParserStateSelectTransition: ParserStateInstance {
|
|||||||
program = statement.evaluate(execution: program)
|
program = statement.evaluate(execution: program)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
switch self.selector.evaluate(execution: program) {
|
switch self.selector.evaluate(execution: program) {
|
||||||
case .Ok(let selector_value):
|
case .Ok(let selector_value):
|
||||||
for (key, target) in zip(self.keys, self.states) {
|
for (key, target) in zip(self.keys, self.states) {
|
||||||
@@ -139,26 +138,32 @@ extension ParserState: Compilable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if state.direct_transition(),
|
if state.direct_transition(),
|
||||||
let transition_statement = state.transition {
|
let transition_statement = state.transition
|
||||||
|
{
|
||||||
return .Ok(
|
return .Ok(
|
||||||
ParserStateDirectTransition(
|
ParserStateDirectTransition(
|
||||||
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
||||||
}
|
}
|
||||||
|
|
||||||
if let transition_select_statement = state.transition,
|
if let transition_select_statement = state.transition,
|
||||||
let transition_select_expression = transition_select_statement.transition_expression {
|
let transition_select_expression = transition_select_statement.transition_expression
|
||||||
|
{
|
||||||
|
|
||||||
var keys: Array<any EvaluatableExpression> = Array()
|
var keys: [any EvaluatableExpression] = Array()
|
||||||
var states: Array<any ParserStateInstance> = Array()
|
var states: [any ParserStateInstance] = Array()
|
||||||
|
|
||||||
for kse in transition_select_expression.keyset_expressions {
|
for kse in transition_select_expression.keyset_expressions {
|
||||||
guard let next_state = current[kse.next_state_name] else {
|
guard let next_state = current[kse.next_state_name] else {
|
||||||
return .Error(Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
return .Error(
|
||||||
|
Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
||||||
}
|
}
|
||||||
keys.append(kse.key)
|
keys.append(kse.key)
|
||||||
states.append(next_state)
|
states.append(next_state)
|
||||||
}
|
}
|
||||||
return .Ok(ParserStateSelectTransition(keys: keys, states: states, selector: transition_select_expression.selector, currrent_state: state))
|
return .Ok(
|
||||||
|
ParserStateSelectTransition(
|
||||||
|
keys: keys, states: states, selector: transition_select_expression.selector,
|
||||||
|
currrent_state: state))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
|
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import P4Lang
|
|
||||||
import Common
|
import Common
|
||||||
|
import P4Lang
|
||||||
|
|
||||||
extension VariableDeclarationStatement: EvaluatableStatement {
|
extension VariableDeclarationStatement: EvaluatableStatement {
|
||||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||||
|
|||||||
Reference in New Issue
Block a user