Skip to content

Commit

Permalink
Merge pull request #79 from makasprzak/proto-3
Browse files Browse the repository at this point in the history
Proto 3
  • Loading branch information
tcripps committed Nov 7, 2015
2 parents 087e99d + ff5961b commit 4f799e8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 18 deletions.
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Expand Up @@ -2,7 +2,7 @@
<id>com.nmatveev.idea-plugin-protobuf</id>
<vendor>Strintec</vendor>
<name>Google Protocol Buffers support</name>
<version>0.5.9</version>
<version>0.5.9.1</version>
<category>Custom Languages</category>
<description>
<![CDATA[
Expand Down
1 change: 1 addition & 0 deletions src/protobuf/lang/PbTokenTypes.java
Expand Up @@ -84,6 +84,7 @@ public interface PbTokenTypes {
IElementType OPTIONAL = new PbElementType("OPTIONAL");
IElementType REPEATED = new PbElementType("REPEATED");
TokenSet FIELD_LABELS = TokenSet.create(REQUIRED,OPTIONAL,REPEATED); //keywords
TokenSet PROTO3_FIELD_LABELS = TokenSet.create(REPEATED); //keywords


IElementType GROUP = new PbElementType("GROUP"); TokenSet GROUP_SET = TokenSet.create(GROUP); //hack for lookAhead
Expand Down
24 changes: 21 additions & 3 deletions src/protobuf/lang/parser/parsing/CompilationUnit.java
Expand Up @@ -14,19 +14,37 @@
public class CompilationUnit implements PbElementTypes {
public static void parse(PbPatchedPsiBuilder builder) {
//parseSeparateOption root level statements
Context context = new Context();
while (!builder.eof()) {
if (SyntaxDeclaration.parse(builder)) {
if (SyntaxDeclaration.parse(builder, context)) {
} else if (PackageDeclaration.parse(builder)) {
} else if (ImportDeclaration.parse(builder)) {
} else if (OptionDeclaration.parseSeparateOption(builder)) {
} else if (ExtendDeclaration.parse(builder)) {
} else if (ExtendDeclaration.parse(builder, context.isProto3())) {
} else if (ServiceDeclaration.parse(builder)) {
} else if (MessageDeclaration.parse(builder)) {
} else if (MessageDeclaration.parse(builder, context.isProto3())) {
} else if (EnumDeclaration.parse(builder)) {
} else if (builder.match(SEMICOLON)) {
} else {
builder.eatError("top.level.def.expected");
}
}
}

public static class Context {
public static final String PROTO3 = "\"proto3\"";
private String syntax;

public String getSyntax() {
return syntax;
}

public void setSyntax(String syntax) {
this.syntax = syntax;
}

public boolean isProto3() {
return PROTO3.equals(syntax);
}
}
}
12 changes: 8 additions & 4 deletions src/protobuf/lang/parser/parsing/ExtendDeclaration.java
Expand Up @@ -10,7 +10,7 @@

public class ExtendDeclaration implements PbElementTypes {

public static boolean parse(PbPatchedPsiBuilder builder) {
public static boolean parse(PbPatchedPsiBuilder builder, boolean proto3Syntax) {
if (!builder.compareToken(EXTEND)) {
return false;
}
Expand All @@ -19,26 +19,30 @@ public static boolean parse(PbPatchedPsiBuilder builder) {
if (!ReferenceElement.parseForCustomType(builder)) {
builder.error("type.expected");
}
if (!parseExtendBlock(builder)) {
if (!parseExtendBlock(builder, proto3Syntax)) {
builder.error("open.block.expected");
}
extendMarker.done(EXTEND_DECL);
return true;
}

public static boolean parseExtendBlock(PbPatchedPsiBuilder builder) {
public static boolean parseExtendBlock(PbPatchedPsiBuilder builder, boolean proto3Syntax) {
if (!builder.compareToken(OPEN_BLOCK)) {
return false;
}
PsiBuilder.Marker extendBlockMarker = builder.mark();
builder.match(OPEN_BLOCK);
while (!builder.eof() && !builder.compareToken(CLOSE_BLOCK)) {
if (!FieldDeclaration.parse(builder)) {
if (!parseField(builder, proto3Syntax)) {
builder.eatError("unexpected.token");
}
}
builder.match(CLOSE_BLOCK, "close.block.expected");
extendBlockMarker.done(EXTEND_BLOCK);
return true;
}

private static boolean parseField(PbPatchedPsiBuilder builder, boolean proto3Syntax) {
return proto3Syntax ? Proto3FieldDeclaration.parse(builder) : FieldDeclaration.parse(builder);
}
}
2 changes: 1 addition & 1 deletion src/protobuf/lang/parser/parsing/FieldDeclaration.java
Expand Up @@ -24,7 +24,7 @@ public static boolean parse(PbPatchedPsiBuilder builder) {
builder.match(EQUAL, "equal.expected");
builder.matchAs(NUM_INT, VALUE, "num.integer.expected");
OptionDeclaration.parseOptionList(builder);
if (!MessageDeclaration.parseMessageBlock(builder)) {
if (!MessageDeclaration.parseMessageBlock(builder, false)) {
builder.error("group.block.expected");
}
messageMarker.done(GROUP_DECL);
Expand Down
16 changes: 8 additions & 8 deletions src/protobuf/lang/parser/parsing/MessageDeclaration.java
Expand Up @@ -10,29 +10,29 @@

public class MessageDeclaration implements PbElementTypes {

public static boolean parse(PbPatchedPsiBuilder builder) {
public static boolean parse(PbPatchedPsiBuilder builder, boolean proto3Syntax) {
if (!builder.compareToken(MESSAGE)) {
return false;
}
PsiBuilder.Marker messageMarker = builder.mark();
builder.match(MESSAGE);
//builder.matchAs(IK,NAME,"identifier.expected");
builder.match(IK, "identifier.expected");
if (!parseMessageBlock(builder)) {
if (!parseMessageBlock(builder, proto3Syntax)) {
builder.error("message.block.expected");
}
messageMarker.done(MESSAGE_DECL);
return true;
}

public static boolean parseMessageBlock(PbPatchedPsiBuilder builder) {
public static boolean parseMessageBlock(PbPatchedPsiBuilder builder, boolean isProto3Syntax) {
if (!builder.compareToken(OPEN_BLOCK)) {
return false;
}
PsiBuilder.Marker blockMarker = builder.mark();
builder.match(OPEN_BLOCK); //matching OPEN_BLOCK
while (!builder.eof() && !builder.compareToken(CLOSE_BLOCK)) {
if (!parseMessageMember(builder)) {
if (!parseMessageMember(builder, isProto3Syntax)) {
builder.eatError("unexpected.token");
}
}
Expand All @@ -41,22 +41,22 @@ public static boolean parseMessageBlock(PbPatchedPsiBuilder builder) {
return true;
}

public static boolean parseMessageMember(PbPatchedPsiBuilder builder) {
public static boolean parseMessageMember(PbPatchedPsiBuilder builder, boolean proto3Syntax) {
//PsiBuilder.Marker statementMarker = builder.mark();
if (builder.match(SEMICOLON)) {
} else if (MessageDeclaration.parse(builder)) {
} else if (MessageDeclaration.parse(builder, proto3Syntax)) {

} else if (OneofDeclaration.parse(builder)) {

} else if (EnumDeclaration.parse(builder)) {

} else if (ExtendDeclaration.parse(builder)) {
} else if (ExtendDeclaration.parse(builder, proto3Syntax)) {

} else if (OptionDeclaration.parseSeparateOption(builder)) {

} else if (parseExtensions(builder)) {

} else if (FieldDeclaration.parse(builder)) {
} else if (proto3Syntax ? Proto3FieldDeclaration.parse(builder) : FieldDeclaration.parse(builder)) {
} else {
//statementMarker.drop();
return false;
Expand Down
58 changes: 58 additions & 0 deletions src/protobuf/lang/parser/parsing/Proto3FieldDeclaration.java
@@ -0,0 +1,58 @@
package protobuf.lang.parser.parsing;

import com.intellij.lang.PsiBuilder;
import protobuf.lang.parser.util.PbPatchedPsiBuilder;

import static protobuf.lang.PbElementTypes.*;

/**
* @author Nikolay Matveev
* Date: Mar 25, 2010
*/
public class Proto3FieldDeclaration {

public static boolean parse(PbPatchedPsiBuilder builder) {
if (builder.lookAhead(PROTO3_FIELD_LABELS, GROUP_SET) || builder.lookAhead(GROUP_SET)) {
PsiBuilder.Marker messageMarker = builder.mark();
builder.match(PROTO3_FIELD_LABELS);
builder.match(GROUP);
builder.match(IK, "identifier.expected");
//builder.matchAs(IK, NAME, "identifier.expected");
builder.match(EQUAL, "equal.expected");
builder.matchAs(NUM_INT, VALUE, "num.integer.expected");
OptionDeclaration.parseOptionList(builder);
if (!MessageDeclaration.parseMessageBlock(builder, true)) {
builder.error("group.block.expected");
}
messageMarker.done(GROUP_DECL);

} else {
PsiBuilder.Marker messageMarker = builder.mark();
builder.match(PROTO3_FIELD_LABELS);
parseType(builder);
builder.match(IK, "identifier.expected");
//builder.matchAs(IK, NAME, "identifier.expected");
builder.match(EQUAL, "equal.expected");
builder.matchAs(NUM_INT, VALUE, "num.integer.expected");
OptionDeclaration.parseOptionList(builder);
builder.match(SEMICOLON, "semicolon.expected");
messageMarker.done(FIELD_DECL);
}
return true;
}

//done
public static boolean parseType(PbPatchedPsiBuilder builder) {
PsiBuilder.Marker marker = builder.mark();
if (builder.match(BUILT_IN_TYPES)) {
} else if (ReferenceElement.parseForCustomType(builder)) {
} else {
marker.drop();
return false;
}
marker.done(FIELD_TYPE);
return true;
}

//done
}
3 changes: 2 additions & 1 deletion src/protobuf/lang/parser/parsing/SyntaxDeclaration.java
Expand Up @@ -13,7 +13,7 @@
*/
public class SyntaxDeclaration implements PbElementTypes {

public static boolean parse(PbPatchedPsiBuilder builder) {
public static boolean parse(PbPatchedPsiBuilder builder, CompilationUnit.Context parsingContext) {
if (!builder.compareToken(SYNTAX)) {
return false;
}
Expand All @@ -22,6 +22,7 @@ public static boolean parse(PbPatchedPsiBuilder builder) {
builder.match(EQUAL, "equal.expected");

PsiBuilder.Marker valueMarker = builder.mark();
parsingContext.setSyntax(builder.getTokenText());
if (builder.match(STRING_LITERALS)) {
valueMarker.done(VALUE);
} else {
Expand Down

2 comments on commit 4f799e8

@daxzel
Copy link

@daxzel daxzel commented on 4f799e8 Dec 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it released? I've downloaded this plugin from a repository and it looks like it still doesn't work with 3.0.0 properly.

@tcripps
Copy link
Collaborator Author

@tcripps tcripps commented on 4f799e8 Dec 12, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.