package org.nextras.orm.intellij.parser;

import com.intellij.lang.PsiBuilder;
import com.jetbrains.php.lang.documentation.phpdoc.lexer.PhpDocTokenTypes;
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocType;
import com.jetbrains.php.lang.parser.PhpPsiBuilder;


public class PhpDocPropertyTagParser extends com.jetbrains.php.lang.documentation.phpdoc.parser.tags.PhpDocPropertyTagParser
{
	public void parse(PhpPsiBuilder builder, boolean inside)
	{
		PsiBuilder.Marker tag = builder.mark();
		builder.match(DOC_TAG_NAME);
		this.parseContents(builder);
		if (!inside) {
			parseValue(builder);
		}

		tag.done(this.getElementType());
	}


	protected static void parseValue(PhpPsiBuilder builder)
	{
		PsiBuilder.Marker value = builder.mark();

		while (true) {
			while (!builder.compare(DOC_LBRACE) && !builder.compare(DOC_TAG_VALUE_END) && !builder.eof()) {
				builder.advanceLexer();
			}

			if (builder.compare(DOC_LBRACE)) {
				PsiBuilder.Marker modifier = builder.mark();
				builder.advanceLexer();
				parseModifierContent(builder);
				modifier.done(PhpDocTypes.phpDocTagModifier);
			} else {
				break;
			}
		}

		// eat the rest
		while (!builder.compare(DOC_TAG_VALUE_END) && !builder.eof()) {
			builder.advanceLexer();
		}

		value.done(phpDocTagValue);
	}


	private static void parseModifierContent(PhpPsiBuilder builder)
	{
		PsiBuilder.Marker modifierName = builder.mark();
		while (!builder.compare(DOC_RBRACE) && !builder.compare(DOC_TAG_VALUE_END) && !builder.eof() && !(builder.getTokenText() != null && builder.getTokenText().equals(" "))) {
			builder.advanceLexer();
		}
		modifierName.done(PhpDocTypes.phpDocTagModifierName);

		PsiBuilder.Marker modifierKey;
		while (true) {
			if (builder.eof() || builder.compare(DOC_TAG_VALUE_END) || builder.compareAndEat(DOC_RBRACE)) {
				break;
			} else if (builder.compare(DOC_IDENTIFIER) || builder.compare(DOC_STRING)) {
				modifierKey = builder.mark();
				builder.advanceLexer();
				modifierKey.done(PhpDocTypes.phpDocTagModifierIdentifier);
			} else {
				builder.advanceLexer();
			}
		}
	}
}

@@ -1576,21 +1576,26 @@
 er.eof()
- && !
+) %7B%0A%09%09%09if 
 (builder
@@ -1621,50 +1621,296 @@
 null
- && builder.getTokenText().equals(%22 %22))) %7B
+) %7B%0A%09%09%09%09// fix %7Bm:1 foo%7D parsed as %5B%22m%22, %22:1 %22, %22foo%22%5D%0A%09%09%09%09String tagText = builder.getTokenText();%0A%09%09%09%09if (tagText.equals(%22 %22)) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09if (tagText.indexOf(':') == 0 && tagText.endsWith(%22 %22) && tagText.length() == 3) %7B%0A%09%09%09%09%09builder.advanceLexer();%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %0A%09%09%09

fix phpdoc parser for tokens ending in \d\w