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