Support for pipe operator syntax for union types

This commit is contained in:
Andrey Vlasovskikh
2013-07-19 18:04:45 +04:00
parent 495a932d4d
commit dbaa838fd7
4 changed files with 17 additions and 5 deletions
@@ -26,6 +26,6 @@ import static com.jetbrains.python.psi.types.PyTypeTokenTypes.*;
[\r\n]+ { return NL; }
[\ \t] { return SPACE; }
(":py"?":class:`"[~!]?)|("`")|([A-Z]"{")|("}") { return MARKUP; }
("or")|("of")|("from")|("to")|[,\(\)\.\[\]] { return OP; }
("or")|("of")|("from")|("to")|[,\(\)\.\[\]|] { return OP; }
[T-Z] { return PARAMETER; }
[A-Za-z_][A-Za-z_0-9]* { return IDENTIFIER; }
@@ -1,4 +1,4 @@
/* The following code was generated by JFlex 1.4.3 on 7/18/13 7:23 PM */
/* The following code was generated by JFlex 1.4.3 on 7/19/13 2:41 PM */
package com.jetbrains.python.psi.types;
@@ -11,7 +11,7 @@ import static com.jetbrains.python.psi.types.PyTypeTokenTypes.*;
/**
* This class is a scanner generated by
* <a href="http://www.jflex.de/">JFlex</a> 1.4.3
* on 7/18/13 7:23 PM from the specification file
* on 7/19/13 2:41 PM from the specification file
* <tt>/home/user/src/idea/tools/lexer/../../python/src/com/jetbrains/python/psi/types/PyType.flex</tt>
*/
public class PyTypeLexer implements FlexLexer {
@@ -39,7 +39,7 @@ public class PyTypeLexer implements FlexLexer {
"\2\0\1\24\1\0\1\24\1\0\12\27\1\3\6\0\23\14\7\25"+
"\1\24\1\0\1\24\1\0\1\26\1\12\1\10\1\26\1\6\2\26"+
"\1\21\5\26\1\7\1\22\1\26\1\17\1\4\1\26\1\20\1\11"+
"\1\23\4\26\1\5\1\26\1\15\1\0\1\16\1\13\uff81\0";
"\1\23\4\26\1\5\1\26\1\15\1\24\1\16\1\13\uff81\0";
/**
* Translates characters to character classes
@@ -226,7 +226,7 @@ public class PyTypeParser {
.named("param-expr");
final FunctionalParser<ParseResult, PyElementType> unionExpr =
paramExpr.then(many(op("or").skipThen(paramExpr)))
paramExpr.then(many(op("or").or(op("|")).skipThen(paramExpr)))
.map(new Function<Pair<ParseResult, List<ParseResult>>, ParseResult>() {
@Override
public ParseResult fun(Pair<ParseResult, List<ParseResult>> value) {
@@ -215,4 +215,16 @@ public class PyTypeParserTest extends PyTestCase {
assertNotNull(second);
assertEquals("int", second.getName());
}
public void testUnionOrOperator() {
myFixture.configureByFile("typeParser/typeParser.py");
final PyUnionType type = (PyUnionType)PyTypeParser.getTypeByName(myFixture.getFile(), "MyObject | str | unicode");
assertNotNull(type);
final Collection<PyType> members = type.getMembers();
assertEquals(3, members.size());
final List<PyType> list = new ArrayList<PyType>(members);
assertClassType(list.get(0), "MyObject");
assertClassType(list.get(1), "str");
assertClassType(list.get(2), "unicode");
}
}