forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9266cbd
commit d246bd6
Showing
5 changed files
with
547 additions
and
0 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
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,99 @@ | ||
#include <core.p4> | ||
#include <v1model.p4> | ||
|
||
const bit<16> TYPE_IPV4 = 0x800; | ||
typedef bit<9> egressSpec_t; | ||
typedef bit<48> macAddr_t; | ||
typedef bit<32> ip4Addr_t; | ||
|
||
header ethernet_t { | ||
macAddr_t dstAddr; | ||
macAddr_t srcAddr; | ||
bit<16> etherType; | ||
} | ||
|
||
header ipv4_t { | ||
bit<4> version; | ||
bit<4> ihl; | ||
bit<8> diffserv; | ||
bit<16> totalLen; | ||
bit<16> identification; | ||
bit<3> flags; | ||
bit<13> fragOffset; | ||
bit<8> ttl; | ||
bit<8> protocol; | ||
bit<16> hdrChecksum; | ||
ip4Addr_t srcAddr; | ||
ip4Addr_t dstAddr; | ||
} | ||
|
||
struct metadata { /* empty */ } | ||
|
||
struct headers { | ||
ethernet_t ethernet; | ||
ipv4_t ipv4; | ||
} | ||
|
||
parser MyParser(packet_in packet, | ||
out headers hdr, | ||
inout metadata meta, | ||
inout standard_metadata_t standard_metadata) { | ||
|
||
state start { transition parse_ethernet; } | ||
|
||
state parse_ethernet { | ||
packet.extract(hdr.ethernet); | ||
transition select(hdr.ethernet.etherType) { | ||
TYPE_IPV4: parse_ipv4; | ||
default: accept; | ||
} | ||
} | ||
|
||
state parse_ipv4 { | ||
packet.extract(hdr.ipv4); | ||
transition accept; | ||
} | ||
|
||
} | ||
|
||
control MyIngress(inout headers hdr, | ||
inout metadata meta, | ||
inout standard_metadata_t standard_metadata) { | ||
action drop() { | ||
mark_to_drop(standard_metadata); | ||
} | ||
|
||
action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) { | ||
standard_metadata.egress_spec = port; | ||
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr; | ||
hdr.ethernet.dstAddr = dstAddr; | ||
hdr.ipv4.ttl = hdr.ipv4.ttl - 1; | ||
} | ||
|
||
table ipv4_lpm { | ||
key = { hdr.ipv4.dstAddr: lpm; } | ||
actions = { ipv4_forward; drop; NoAction; } | ||
size = 1024; | ||
default_action = drop(); | ||
} | ||
|
||
apply { | ||
if (hdr.ipv4.isValid()) { | ||
ipv4_lpm.apply(); | ||
} | ||
} | ||
} | ||
|
||
|
||
control MyDeparser(packet_out packet, in headers hdr) { | ||
apply { | ||
packet.emit(hdr.ethernet); | ||
packet.emit(hdr.ipv4); | ||
} | ||
} | ||
|
||
V1Switch( | ||
MyParser(), | ||
MyIngress(), | ||
MyDeparser() | ||
) main; |
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,75 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
module Rouge | ||
module Lexers | ||
class P4 < RegexLexer | ||
tag 'p4' | ||
title 'P4' | ||
desc 'The P4 programming language' | ||
filenames '*.p4' | ||
mimetypes 'text/x-p4' | ||
|
||
def self.keywords | ||
@keywords ||= Set.new %w( | ||
abstract action actions apply const default default_action else enum | ||
extract extern exit if in inout key list out package packet_in return | ||
select switch this transition tuple type typedef verify | ||
) | ||
end | ||
|
||
def self.operators | ||
@operators ||= Set.new %w( | ||
\|\+\| \|-\| \? \& \&\&\& < > << >> \* \| ~ \^ - \+ / | ||
\# \. = != <= >= \+\+ | ||
) | ||
end | ||
|
||
def self.decls | ||
@decls ||= Set.new %w( | ||
control header header_union parser entries state struct table | ||
value_set | ||
) | ||
end | ||
|
||
def self.builtins | ||
@builtins ||= Set.new %w( | ||
void error string match_kind bool int bit varbit | ||
) | ||
end | ||
|
||
state :whitespace do | ||
rule %r/\s+/m, Text | ||
rule %r((//).*$\n?), Comment::Single | ||
rule %r/\/\*(?:(?!\*\/).)*\*\//m, Comment::Multiline | ||
end | ||
|
||
state :number do | ||
rule %r/([0-9]+[sw])?0[oO][0-7_]+/, Num | ||
rule %r/([0-9]+[sw])?0[xX][0-9a-fA-F_]+/, Num | ||
rule %r/([0-9]+[sw])?0[bB][01_]+/, Num | ||
rule %r/([0-9]+[sw])?0[dD][0-9_]+/, Num | ||
rule %r/([0-9]+[sw])?[0-9_]+/, Num | ||
end | ||
|
||
id = /[\p{XID_Start}_]\p{XID_Continue}*/ | ||
string_element = /\\"|[^"]/x | ||
|
||
state :root do | ||
mixin :whitespace | ||
|
||
rule %r/#\s*#{id}/, Comment::Preproc | ||
rule %r/\b(?:#{P4.keywords.join('|')})\b/, Keyword | ||
rule %r/\b(?:#{P4.decls.join('|')})\b/, Keyword::Declaration | ||
rule %r/\b(?:#{P4.builtins.join('|')})\b/, Name::Builtin | ||
rule %r/\b#{id}_[th]\b/x, Name::Class | ||
rule %r/(?:#{P4.operators.join('|')})/x, Operator | ||
rule %r/[(){}\[\]<>,:;\.]/, Punctuation | ||
mixin :number | ||
rule %r/@#{id}/x, Name::Label | ||
rule %r/#{id}/x, Text | ||
rule %r/"(?: #{string_element} )*"/x, Str::String | ||
end | ||
end | ||
end | ||
end |
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,18 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
describe Rouge::Lexers::P4 do | ||
let(:subject) { Rouge::Lexers::P4.new } | ||
|
||
describe 'guessing' do | ||
include Support::Guessing | ||
|
||
it 'guesses by filename' do | ||
assert_guess :filename => 'foo.p4' | ||
end | ||
|
||
it 'guesses by mimetype' do | ||
assert_guess :mimetype => 'text/x-p4' | ||
end | ||
end | ||
end |
Oops, something went wrong.