use of org.python.pydev.core.docutils.SyntaxErrorException in project Pydev by fabioz.
the class PyFormatter method formatForPar.
/**
* Formats the contents for when a parenthesis is found (so, go until the closing parens and format it accordingly)
* @param throwSyntaxError
* @throws SyntaxErrorException
*/
private static int formatForPar(final ParsingUtils parsingUtils, final char[] cs, final int i, final FormatStd std, final FastStringBuffer buf, final int parensLevel, final String delimiter, boolean throwSyntaxError) throws SyntaxErrorException {
char c = ' ';
FastStringBuffer locBuf = new FastStringBuffer();
int j = i + 1;
int start = j;
int end = start;
while (j < cs.length && (c = cs[j]) != ')') {
j++;
if (c == '\'' || c == '"') {
// ignore comments or multiline comments...
j = parsingUtils.eatLiterals(null, j - 1, std.trimMultilineLiterals) + 1;
end = j;
} else if (c == '#') {
j = parsingUtils.eatComments(null, j - 1) + 1;
end = j;
} else if (c == '(') {
// open another par.
if (end > start) {
locBuf.append(cs, start, end - start);
start = end;
}
j = formatForPar(parsingUtils, cs, j - 1, std, locBuf, parensLevel + 1, delimiter, throwSyntaxError) + 1;
start = j;
} else {
end = j;
}
}
if (end > start) {
locBuf.append(cs, start, end - start);
start = end;
}
if (c == ')') {
// Now, when a closing parens is found, let's see the contents of the line where that parens was found
// and if it's only whitespaces, add all those whitespaces (to handle the following case:
// a(a,
// b
// ) <-- we don't want to change this one.
char c1;
FastStringBuffer buf1 = new FastStringBuffer();
if (locBuf.indexOf('\n') != -1 || locBuf.indexOf('\r') != -1) {
for (int k = locBuf.length(); k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n' && c1 != '\r'; k--) {
buf1.insert(0, c1);
}
}
FastStringBuffer formatStr = formatStr(trim(locBuf).toString(), std, parensLevel, delimiter, throwSyntaxError);
FastStringBuffer formatStrBuf = trim(formatStr);
String closing = ")";
if (buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())) {
formatStrBuf.append(buf1);
} else if (std.parametersWithSpace) {
closing = " )";
}
if (std.parametersWithSpace) {
if (formatStrBuf.length() == 0) {
buf.append("()");
} else {
buf.append("( ");
buf.append(formatStrBuf);
buf.append(closing);
}
} else {
buf.append('(');
buf.append(formatStrBuf);
buf.append(closing);
}
return j;
} else {
if (throwSyntaxError) {
throw new SyntaxErrorException("No closing ')' found.");
}
// we found no closing parens but we finished looking already, so, let's just add anything without
// more formatting...
buf.append('(');
buf.append(locBuf);
return j;
}
}
use of org.python.pydev.core.docutils.SyntaxErrorException in project Pydev by fabioz.
the class FastDefinitionsParser method parse.
public static SimpleNode parse(char[] cs, String moduleName, int len, File f) {
FastDefinitionsParser parser = new FastDefinitionsParser(cs, len, moduleName, f);
try {
parser.extractBody();
} catch (SyntaxErrorException e) {
throw new RuntimeException(e);
} catch (StackOverflowError e) {
RuntimeException runtimeException = new RuntimeException(e);
Log.log("Error parsing: " + moduleName + " - " + f + "\nContents:\n" + new String(cs, 0, len > 1000 ? 1000 : len), // report at most 1000 chars...
runtimeException);
throw runtimeException;
}
List<stmtType> body = parser.body;
Module ret = new Module(body.toArray(new stmtType[body.size()]));
ret.beginLine = 1;
ret.beginColumn = 1;
if (parseCallbacks.size() > 0) {
Tuple<String, SimpleNode> arg = new Tuple<String, SimpleNode>(moduleName, ret);
for (ICallback<Object, Tuple<String, SimpleNode>> c : parseCallbacks) {
c.call(arg);
}
}
return ret;
}
use of org.python.pydev.core.docutils.SyntaxErrorException in project Pydev by fabioz.
the class ScopesParser method createScopes.
@Override
public Scopes createScopes(IDocument doc) {
this.scopes = new Scopes();
this.doc = doc;
this.lineOffsetToIndent = new TreeMap<Integer, Integer>();
try {
TabNannyDocIterator nannyDocIterator = new TabNannyDocIterator(doc, true, false);
while (nannyDocIterator.hasNext()) {
IndentInfo next = nannyDocIterator.next();
this.lineOffsetToIndent.put(next.startOffset, next.indent.length());
}
} catch (BadLocationException e1) {
throw new RuntimeException(e1);
}
try {
return this.createScopes();
} catch (SyntaxErrorException e) {
throw new RuntimeException(e);
}
}
use of org.python.pydev.core.docutils.SyntaxErrorException in project Pydev by fabioz.
the class ScopesParser method createInternalScopes.
private int createInternalScopes(ParsingUtils parsingUtils, int offsetDelta) {
int docLen = parsingUtils.len();
int offset = 0;
FastStringBuffer lineMemo = new FastStringBuffer();
int memoStart = 0;
int id;
for (; offset < docLen; offset++) {
char ch = parsingUtils.charAt(offset);
switch(ch) {
case '#':
id = this.scopes.startScope(offsetDelta + offset, Scopes.TYPE_COMMENT);
offset = parsingUtils.eatComments(null, offset);
this.scopes.endScope(id, offsetDelta + offset, Scopes.TYPE_COMMENT);
break;
case '{':
case '[':
case '(':
int baseOffset = offset;
try {
// If a SyntaxError is raised here, we won't create a scope!
offset = parsingUtils.eatPar(offset, null, ch);
id = this.scopes.startScope(offsetDelta + baseOffset + 1, Scopes.TYPE_PEER);
try {
String cs = doc.get(offsetDelta + baseOffset + 1, offset - baseOffset - 1);
createInternalScopes(ParsingUtils.create(cs, true), offsetDelta + baseOffset + 1);
} catch (BadLocationException e1) {
Log.log(e1);
}
this.scopes.endScope(id, offsetDelta + offset, Scopes.TYPE_PEER);
} catch (SyntaxErrorException e2) {
}
break;
case '\'':
case '\"':
baseOffset = offset;
try {
// If a SyntaxError is raised here, we won't create a scope!
offset = parsingUtils.eatLiterals(null, offset);
id = this.scopes.startScope(offsetDelta + baseOffset, Scopes.TYPE_STRING);
this.scopes.endScope(id, offsetDelta + offset + 1, Scopes.TYPE_STRING);
} catch (SyntaxErrorException e1) {
}
break;
case ':':
if (PySelection.startsWithIndentToken(lineMemo.toString().trim())) {
SortedMap<Integer, Integer> subMap = lineOffsetToIndent.tailMap(offsetDelta + memoStart + 1);
Integer level = lineOffsetToIndent.get(offsetDelta + memoStart);
if (level == null) {
// It's a ':' inside a parens
continue;
}
Set<Entry<Integer, Integer>> entrySet = subMap.entrySet();
boolean found = false;
id = this.scopes.startScope(memoStart + level, Scopes.TYPE_SUITE);
int id2 = -1;
for (int j = offset + 1; j < docLen; j++) {
char c = parsingUtils.charAt(j);
if (Character.isWhitespace(c)) {
continue;
}
if (c == '#') {
j = parsingUtils.eatComments(null, j);
continue;
}
id2 = this.scopes.startScope(offsetDelta + j, Scopes.TYPE_SUITE);
break;
}
for (Entry<Integer, Integer> entry : entrySet) {
if (level >= entry.getValue()) {
found = true;
Integer scopeEndOffset = entry.getKey();
try {
int line = doc.getLineOfOffset(scopeEndOffset);
if (line > 0) {
// We want it to end at the end of the previous line (not at the start of the next scope)
IRegion lineInformation = doc.getLineInformation(line - 1);
scopeEndOffset = lineInformation.getOffset() + lineInformation.getLength();
}
} catch (BadLocationException e) {
Log.log(e);
}
this.scopes.endScope(id, scopeEndOffset, Scopes.TYPE_SUITE);
if (id2 > 0) {
this.scopes.endScope(id2, scopeEndOffset, Scopes.TYPE_SUITE);
}
break;
}
}
if (!found) {
// Ends at the end of the document!
this.scopes.endScope(id, offsetDelta + parsingUtils.len(), Scopes.TYPE_SUITE);
if (id2 > 0) {
this.scopes.endScope(id2, offsetDelta + parsingUtils.len(), Scopes.TYPE_SUITE);
}
}
}
break;
case '\r':
case '\n':
// Note that we don't add the \r nor \n to the memo (but we clear it if the line did not end with a \).
if (lineMemo.length() > 0 && lineMemo.lastChar() != '\\') {
lineMemo.clear();
}
break;
default:
if (lineMemo.length() == 0) {
memoStart = offset;
}
lineMemo.append(ch);
break;
}
}
return offset;
}
use of org.python.pydev.core.docutils.SyntaxErrorException in project Pydev by fabioz.
the class PyPeerLinker method isLiteralBalanced.
/**
* @return true if the passed string has balanced ' and "
*/
private boolean isLiteralBalanced(String cursorLineContents) {
ParsingUtils parsingUtils = ParsingUtils.create(cursorLineContents, true);
int offset = 0;
int end = cursorLineContents.length();
boolean balanced = true;
while (offset < end) {
char curr = cursorLineContents.charAt(offset++);
if (curr == '"' || curr == '\'') {
int eaten;
try {
eaten = parsingUtils.eatLiterals(null, offset - 1) + 1;
} catch (SyntaxErrorException e) {
balanced = false;
break;
}
if (eaten > offset) {
offset = eaten;
}
}
}
return balanced;
}
Aggregations