package me.coley.recaf.parse.assembly.visitors;

import me.coley.recaf.parse.assembly.LineParseException;
import me.coley.recaf.parse.assembly.parsers.*;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode;

import java.util.List;

/**
 * Integer type instruction parser.
 *
 * @author Matt
 */
public class LdcVisitor extends InstructionVisitor {
	/**
	 * @param asm
	 * 		Parent assembly visitor for feeding back parsed content.
	 */
	public LdcVisitor(AssemblyVisitor asm) {
		super(asm);
		addSection(new MultiParser("value",
				new NumericParser("value"),
				new StringParser(),
				new DescriptorParser(DescriptorParser.DescType.FIELD)));
	}

	@Override
	public void visit(String text) throws LineParseException {
		List<Object> args = parse(text);
		Object value = args.get(1);
		if (value instanceof String) {
			String valueStr =  (String) value;
			// Either a string or type
			// - Strings wrapped in "
			// - Otherwise should be a type
			if (text.contains("\"")) {
				valueStr = valueStr.replace("\\r", "\r");
				valueStr = valueStr.replace("\\n", "\n");
				valueStr = valueStr.replace("\\t", "\t");
				value = valueStr;
			} else {
				value = Type.getType(valueStr);
			}
		}
		asm.appendInsn(new LdcInsnNode(value));
	}
}

@@ -300,15 +300,16 @@
 %0A * 
-Integer
+Constant
  typ
@@ -1040,16 +1040,47 @@
 %5C%22%22)) %7B%0A
+%09%09%09%09// Escape common sequences%0A
 %09%09%09%09valu

Fix ldc header docs