Skip to content

Commit

Permalink
Add parsing for parsing indentation.
Browse files Browse the repository at this point in the history
git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@101 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
  • Loading branch information
[email protected] committed Jan 23, 2015
1 parent 98b4528 commit c5ad6aa
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/edu/lsu/cct/piraha/BodyWhite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.lsu.cct.piraha;

import edu.lsu.cct.util.Here;

public class BodyWhite extends Pattern {

@Override
public boolean match(Matcher m) {
int pos = m.getTextPos();
if(pos==0 || m.text.charAt(pos-1)=='\n') {
int newPos = pos;
for(;newPos<m.text.length();newPos++) {
char c = m.text.charAt(newPos);
if(c == ' '||c == '\t') {
int n = newPos - pos;
if(n < m.white.length()) {
if(c != m.white.charAt(n)) {
return false;
}
if(n+1 == m.white.length()) {
m.setTextPos(newPos+1);
return true;
}
} else {
return false;
}
} else {
return false;
}
}
}
return false;
}

@Override
public String decompile() {
return "{BodyWhite}";
}

@Override
public boolean eq(Object obj) {
return obj instanceof BodyWhite;
}

}
51 changes: 51 additions & 0 deletions src/edu/lsu/cct/piraha/CloseWhite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package edu.lsu.cct.piraha;

import edu.lsu.cct.util.Here;


public class CloseWhite extends Pattern {

@Override
public boolean match(Matcher m) {
int pos = m.getTextPos();
if(pos==0 || m.text.charAt(pos-1)=='\n') {
int thresh = 0;
int nt = m.whiteThresholds.size();
if(nt > 0) {
thresh = m.whiteThresholds.get(nt-1);
}
int newPos = pos;
for(;newPos<m.text.length();newPos++) {
char c = m.text.charAt(newPos);
if(c == ' '||c == '\t') {
;
} else {
break;
}
}
if(newPos-pos <= thresh) {
int n = m.whiteThresholds.remove(nt-1);
if(nt == 1)
m.white.setLength(0);
else
m.white.setLength(m.whiteThresholds.get(nt-2));
return true;
} else {
return false;
}
} else {
return false;
}
}

@Override
public String decompile() {
return "{CloseWhite}";
}

@Override
public boolean eq(Object obj) {
return obj instanceof CloseWhite;
}

}
48 changes: 48 additions & 0 deletions src/edu/lsu/cct/piraha/OpenWhite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.lsu.cct.piraha;

public class OpenWhite extends Pattern {

@Override
public boolean match(Matcher m) {
int pos = m.getTextPos();
if(pos==0 || m.text.charAt(pos-1)=='\n') {
int newPos = pos;
boolean newWhite = false;
StringBuilder saveWhite = new StringBuilder();
for(;newPos<m.text.length();newPos++) {
char c = m.text.charAt(newPos);
if(c == ' '||c == '\t') {
int n = newPos - pos;
if(n < m.white.length()) {
if(c != m.white.charAt(n)) {
return false;
}
} else {
saveWhite.append(c);
newWhite = true;
}
} else {
break;
}
}
if(newWhite) {
m.whiteThresholds.add(newPos-pos);
m.white.append(saveWhite);
}
return newWhite;
} else {
return false;
}
}

@Override
public String decompile() {
return "{OpenWhite}";
}

@Override
public boolean eq(Object obj) {
return obj instanceof OpenWhite;
}

}

0 comments on commit c5ad6aa

Please sign in to comment.